How to check that radiobuttonlist has a selected value




How to check that radiobuttonlist has a selected value If you've you are using a radiobuttonlist in a form and that list isn't compulsory, then it's important that you check to see if a value has been selected before you do anything (i.e. save the completed form to a database etc).

This can easily be done.  Lets say you radio button list looks a little like this:

<asp:RadioButtonList id="rblOption" runat="server">
   <asp:ListItem Value="1" Text="Yes" />
   <asp:ListItem Value="0" Text="No" />
</asp:RadioButtonList>
Then to check if a value has been selected, you would use some code like this:
If rblOption.SelectedIndex <> -1 Then
   strOption = rblOption.SelectedItem.Value
Else
   strOption = ""
End If
That's all there is to it, so now you won't get any errors in your forms if no value is selected.

12 people like this article, click here to say you like this article too (12)
Send a link to this page via email Send a link to this page