我在webForm中添加了一个RadioButtonList控件,带有5个item,运行的时候,如果你选择了一个的话,运行正常,可是,如果一个都没有选择,则出现error。
 
我用button做的提交。
错误如下
未将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
行 58:     else {RadioLabel.Text=MyJobRb.Text;}
行 59:  //如果选择了mysturb,则RadioLabel的文本为MyJobRb.Text;
行 60:  if (MyRBList.SelectedItem.Selected==true)
行 61:  { ListLabel.Text=MyRBList.SelectedItem.Text;}
行 62:  else ListLabel.Text="111";
 源文件: c:\inetpub\wwwroot\aspx\singmutibuttons\webform1.aspx.cs    行: 60 堆栈跟踪: 
[NullReferenceException: 未将对象引用设置到对象的实例。]
   SingMutiButtons.WebForm1.SureBtn_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\aspx\singmutibuttons\webform1.aspx.cs:60
   System.Web.UI.WebControls.Button.OnClick(EventArgs e)
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   System.Web.UI.Page.ProcessRequestMain() 

解决方案 »

  1.   

    tryif (MyRBList.SelectedItem.Selected==true)
    ===>
    if (MyRBList.SelectedIndex>=0)
      

  2.   

    等了这么久,只有思归大哥给我回答,(虽然我在不断的调试下已经做好了)非常感谢。到底是5星啊,对谁都那么认真。 好,这20分都给你了思归大哥。(NND,如果知道会碰上你的话我就送100分得了(我只剩120分了))感激之至
      

  3.   

    大哥,你的对了。牛人。
    不过大哥,我将MyRBList的任意一个item的selected 设置为true重新运行也是可以的。
    这2种方法有何区别呢?MyRBList.SelectedIndex>=0中>=0怎么解?
      

  4.   

    if nothing in the radiobuttonlist is selected,SelectedIndex will be -1>=0 just means something is selected
      

  5.   

    oh!saucer(思归) ,you are right!