place your code like this:
if (Request.Form["Gender"] != null)
{
string selectedGender = Request.Form["Gender"].ToString();
}
Note that Request.Form["Gender"]
will be null if none of the RadioButtons are selected.
see the markup below
<form id="form1" runat="server" method="post">
<input type="radio" name="Gender" value="male" id="test" checked="checked" />
male
<input type="radio" name="Gender" value="female" />female
<input type="submit" value="test" />
<asp:Button ID="btn" runat="server" Text="value" />
</form>
for both the buttons i.e input type="submit"
and usual asp:button
, Request.Form["Gender"]
is going to have some value upon PostBack
, provided, either of the RadioButtons is selected.
And yes, upon PostBack
only, i.e. when you hit either of the buttons and not on first load.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…