Search code examples
asp.netvb.netradio-buttongroupname

asp.net - get the text of the selected item in a radiobutton group


I've found this code, but I was wondering whether there's a more streamlined way to do it.

So for example, rather than having all the if statements can you have one line that says Label1.text = "You selected " & RadioGroup1.Text

  Sub SubmitBtn_Click(Sender As Object, e As EventArgs)
     If Radio1.Checked Then
        Label1.Text = "You selected " & Radio1.Text
     ElseIf Radio2.Checked Then
        Label1.Text = "You selected " & Radio2.Text
     ElseIf Radio3.Checked Then
        Label1.Text = "You selected " & Radio3.Text
     End If
  End Sub

and

   <asp:RadioButton id=Radio1 Text="Typical" Checked="True" GroupName="RadioGroup1" runat="server" />

   <asp:RadioButton id=Radio2 Text="Compact" GroupName="RadioGroup1" runat="server"/>

   <asp:RadioButton id=Radio3 Text="Full" GroupName="RadioGroup1" runat="server" />

   <asp:Button text="Submit" OnClick="SubmitBtn_Click" runat=server/>

   <asp:Label id=Label1 Font-Bold="true"  runat="server" />

Solution

  • You can use a <asp:radiobuttonlist> have it's id as radio1. Then have seperate <asp:listitem> in there. Set each list item with a different value, ie, Typical, Contact, Full. Then all you would need in SubmitBtn_Click would be radio1.SelectedItem.Value