Brandon
Brandon

Reputation:

How to get the selected value from RadioButtonList?

I have a RadioButtonList on my page that is populated via Data Binding

<asp:RadioButtonList ID="rb" runat="server">
</asp:RadioButtonList>
<asp:Button Text="Submit" OnClick="submit" runat="server" />

How do I get the value of the radio button that the user selected in my "submit" method?

Upvotes: 36

Views: 150547

Answers (5)

Leon Faircloth
Leon Faircloth

Reputation: 1

Technically speaking the answer is correct, but there is a potential problem remaining. string test = rb.SelectedValue is an object and while this implicit cast works. It may not work correction if you were sending it to another method (and granted this may depend on the version of framework, I am unsure) it may not recognize the value.

string test = rb.SelectedValue;  //May work fine
SomeMethod(rb.SelectedValue);

where SomeMethod is expecting a string may not.

Sadly the rb.SelectedValue.ToString(); can save a few unexpected issues.

Upvotes: 0

terjetyl
terjetyl

Reputation: 9565

Using your radio button's ID, try rb.SelectedValue.

Upvotes: 13

dizad87
dizad87

Reputation: 468

string radioListValue = RadioButtonList.Text;

Upvotes: -2

Pushkar Phule
Pushkar Phule

Reputation: 170

radiobuttonlist.selected <value> to process with your code.

Upvotes: -5

azamsharp
azamsharp

Reputation: 20066

The ASPX code will look something like this:

 <asp:RadioButtonList ID="rblist1" runat="server">

    <asp:ListItem Text ="Item1" Value="1" />
    <asp:ListItem Text ="Item2" Value="2" />
    <asp:ListItem Text ="Item3" Value="3" />
    <asp:ListItem Text ="Item4" Value="4" />

    </asp:RadioButtonList>

    <asp:Button ID="btn1" runat="server" OnClick="Button1_Click" Text="select value" />

And the code behind:

protected void Button1_Click(object sender, EventArgs e)
        {
            string selectedValue = rblist1.SelectedValue;
            Response.Write(selectedValue);
        }

Upvotes: 53

Related Questions