What is the best way to see if a RadioButtonList has a selected value?

John Bubriski picture John Bubriski · Apr 9, 2009 · Viewed 37.6k times · Source

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))

Answer

Andrew Hare picture Andrew Hare · Apr 9, 2009

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.