I am using:
if (RadioButtonList_VolunteerType.SelectedItem != null)
or how about:
if (RadioButtonList_VolunteerType.Index >= 0)
or how about (per Andrew Hare's answer):
if (RadioButtonList_VolunteerType.Index > -1)
To those who may read this question, the following is not a valid method. As Keltex pointed out, the selected value could be an empty string.
if (string.IsNullOrEmpty(RadioButtonList_VolunteerType.SelectedValue))
You can assgin the onclick event for the RadioButtonList, when you click one rediobutton, it will fire the GetRadioButtonValue fuction. It will find the checked rediobutton, and move its value into textbox.
getElementById("<%=RadioButtonList1. ClientID%>"); var flag=false; for (var i = 0; i < rbl1. length; i++) { if (radio[i]. checked) flag=true; } if(flag==true) alert('radiobuttonlist1 is selected');
Checking if any of them are checked could be done like this: var radioButtonCheckedInGr1 = GetRadioButtons(Form, "Gr1"). Any(i => i. Checked);
Those are all valid and perfectly legitimate ways of checking for a selected value. Personally I find
RadioButtonList_VolunteerType.SelectedIndex > -1
to be the clearest.
In terms of readability they all lack something for me. This seems like a good candidate for an extension method.
public static class MyExtenstionMethods
{
public static bool HasSelectedValue(this RadioButtonList list)
{
return list.SelectedItem != null;
}
}
...
if (RadioButtonList_VolunteerType.HasSelectedValue)
{
// do stuff
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With