user3437460
user3437460

Reputation: 17454

ASP.NET Get value from Radiobuttonlist

I have the following radionbuttonlist in the ASPX:

Line 62:    <td class="auto-style6">2 Gender</td>
Line 63:    <td>
Line 64:    <asp:RadioButtonList ID="rblGender" runat="server" RepeatDirection="Horizontal" OnSelectedIndexChanged="rblGender_SelectedIndexChanged">
Line 65:        <asp:ListItem Value="Male">Male</asp:ListItem>
Line 66:            <asp:ListItem Value="Female">Female</asp:ListItem>
Line 67:        </asp:RadioButtonList>
Line 68:    </td>

I am trying to get the selected value of a checked radio button (Male/Female) and display it in a textfield when I click a button.

I thought it should be something really easy, and I have the following code in c#.

protected void btnTest_Click(object sender, EventArgs e)
{
    txtGender.Text = rblGender.SelectedValue;
}

Everything can compile without any errors. However when I run the web form in the browser and click the btnTest, the browser shows the following error: It also highlighted the above code snippet at line 64 in red.

Compiler Error Message: CS1061: 'ASP.userForm_aspx' does not contain a definition for 'rblGender_SelectedIndexChanged' and no extension method 'rblGender_SelectedIndexChanged' accepting a first argument of type 'ASP.userForm_aspx' could be found (are you missing a using directive or an assembly reference?)

Question: What did I missed out or done wrongly?

There are other questions with this topic in SO, however theirs could be easily fixed using .selectedValue. When I tried mine, it gives me the above error.

Upvotes: 0

Views: 1422

Answers (2)

user3437460
user3437460

Reputation: 17454

I also found the cause of the problem actually.

It was because of "OnSelectedIndexChanged="rblGender_SelectedIndexChanged", they are expecting me to write a method in C# to handle radiobutton state change but I didn't include that in my C# codes.

When I remove it and write is as:

<asp:RadioButtonList ID="rblGender" runat="server" RepeatDirection="Horizontal">

everything works fine.

Upvotes: 1

Mark Tomlinson
Mark Tomlinson

Reputation: 127

This is very simple update your onselectedindexchanged event to point to btnTest_Click or setup the event you have named in onselectedindexchanged: rblGender_SelectedIndexChanged. The error is just saying you dont have the event wired up correctly.

Upvotes: 1

Related Questions