我想找到一種方法來在用戶沒有在下拉列表中選擇一個值時拋出一個錯誤。我嘗試了很多解決方案,這裏提供了。但是,沒有人可以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