我想找到一种方法来在用户没有在下拉列表中选择一个值时抛出一个错误。我尝试了很多解决方案,这里提供了。但是,没有人可以work.This是我的代码NullException在RadioButtonList中没有选择任何值时的错误
protected void Button1_Click(object sender, EventArgs e)
{
if (RadioButtonList1.SelectedItem.Value == null)
{
//Throw error to select some value before button click
}
if (RadioButtonList1.SelectedItem.Value == 'male')
{
//Step1
}
if (RadioButtonList1.SelectedItem.Value == 'female')
{
//Step2
}
}
试图与
if (RadioButtonList1.SelectedIndex == -1)
更换,但同样没有工作。有任何想法吗?从评论
如果没有选择,什么*是* SelectedIndex,如果不是'-1'?当你调试时,实际价值是多少? – David
“RadioButtonList1.SelectedItem.Value == null”的作品? SelectedItem应该为空。 –
执行代码仅在选择单选按钮时执行,当用户单击时没有选择时出现错误 - 对象引用未设置为对象的实例...并且没有“RadioButtonList1.SelectedItem.Value”== null不工作 – rakesh