Reputation: 41
I have a label and two radio buttons, i am using post method to and trying to get the value of the radio button but i have no clue to do it
<div class="input-group">
<label>Enter Your Age</label>
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
</div>
<div class="input-group">
<label>Select Your Gender</label>
<asp:RadioButton ID="RadioButton1" runat="server" GroupName="Gender" Text="Male" Checked="True" />
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="Gender" Text="Female" />
</div>
<asp:Button ID="submit" runat="server" Text="Submit" PostBackUrl="~/receivePage.aspx" OnClick="submit_Click" />
and in my receive page
String age = ((TextBox)PreviousPage.FindControl("txtAge")).Text;
this gives me the age value but how do I get the radio buttons value ?
Upvotes: 0
Views: 2163
Reputation: 73761
You could use a RadioButtonList instead of two separate RadioButtons:
<asp:RadioButtonList ID="rblGender" runat="server">
<asp:ListItem Text="Male" Selected="True" />
<asp:ListItem Text="Female" />
</asp:RadioButtonList>
In code-behind, you could get the SelectedValue of the list:
String gender = ((RadioButtonList)PreviousPage.FindControl("rblGender")).SelectedValue;
Upvotes: 1
Reputation: 111
Try:
<div class="input-group">
<label>Select Your Gender</label>
<asp:RadioButton ID="RadioButton1" runat="server" GroupName="Gender" Text="Male" Checked="True" value="Male" />
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="Gender" Text="Female" value="Female" />
</div>
You need to state a value for each radio button.
Upvotes: 1