如何在asp.net的下一页中获取单选按钮值

时间:2016-03-27 10:07:08

标签: asp.net

我有一个标签和两个单选按钮,我正在使用post方法并尝试获取单选按钮的值,但我不知道这样做

 <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" />

并在我的接收页面中

 String age = ((TextBox)PreviousPage.FindControl("txtAge")).Text;

这给了我年龄值但是如何获得单选按钮值?

2 个答案:

答案 0 :(得分:1)

尝试:

<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>

您需要为每个单选按钮说明一个值。

答案 1 :(得分:1)

您可以使用RadioButtonList而不是两个单独的RadioButtons:

<asp:RadioButtonList ID="rblGender" runat="server">
    <asp:ListItem Text="Male" Selected="True" />
    <asp:ListItem Text="Female" />
</asp:RadioButtonList>

在代码隐藏中,您可以获得列表的SelectedValue:

String gender = ((RadioButtonList)PreviousPage.FindControl("rblGender")).SelectedValue;