【问题标题】:Radio button list selected item value issu单选按钮列表选定项目值问题
【发布时间】:2014-09-05 11:16:53
【问题描述】:

我有一个单选按钮列表,通过这种方法将其与数据绑定:

 DataTable dt1 = BeeStatus.GetAllBeeStatus();
 radio1.DataSource = dt1;
 radio1.DataTextField = "beeStatus";
 radio1.DataValueField = "beestatusID";
 radio1.DataBind();

当我尝试获取所选项目的值时,通过编写:

int st = Convert.ToInt32( radio1.SelectedItem.value);

它给出了这个错误:

[“/”应用程序中的服务器错误。

对象引用未设置为对象的实例。

Description: An unhandled exception occurred during the execution of the current 
             web request. 
Please review the stack trace for more information about the error and where 
     it originated in the code. ]

有什么帮助吗?

异常详情: System.NullReferenceException:对象引用未设置为对象的实例。

【问题讨论】:

  • radio1.SelectedItem.value 的某些部分为空,在调试器中检查它,例如如果没有选择任何值,那么 radio1.SelectedItem 将为空。在尝试转换为 int 之前,您需要进行一次空值检查。
  • 我已经检查过了,同样的错误。

标签: c# asp.net radiobuttonlist


【解决方案1】:

让我猜猜:您也在回发时对RadioButtonList 进行数据绑定,这会阻止事件被触发。检查IsPostBack 属性:

protected void Page_Load(Object sender, EventArgs e)
{
    if(!IsPostBack)
    {
         DataTable dt1 = BeeStatus.GetAllBeeStatus();
         radio1.DataSource = dt1;
         radio1.DataTextField = "beeStatus";
         radio1.DataValueField = "beestatusID";
         radio1.DataBind();
    }
}

否则radio1.SelectedItemnull,因为不再选择任何内容。

【讨论】:

  • no tim,单选按钮被选中但没有值。
  • @alim91:你在radio1.SelectedItem.Value 得到了异常,因为radio1.SelectedItemnull,所以你不能访问item.Value。不再选择它的一个原因是您之前曾调用过radio1.DataBind();。这就是为什么我建议使用IsPostBack-property。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-17
  • 2013-03-09
  • 1970-01-01
相关资源
最近更新 更多