2016-03-27 81 views
0

我有一個標籤和兩個單選按鈕,我使用post方法並嘗試獲取單選按鈕的值,但我不知道它如何在asp.net中的下一頁獲取單選按鈕值

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

這給了我年齡的價值,但如何獲取單選按鈕的價值?

回答

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

你可以使用一個單選按鈕列表,而不是兩個單獨的單選按鈕:

<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; 
相關問題