问题描述
我有以下 HTML 源代码
I have the following HTML source
<form name="Register1" action="Register.aspx" id="registerform" method="post"
runat="server" style="margin-top: 15px;">
<input type="radio" name="Gender" value="male" />male
<input type="radio" name="Gender" value="female" />female
</form>
我的问题是如何在 c# 页面中获取所选值到变量?
My question is how can I get the selected value to variable in the c# page?
我试过这个:
Gender = Request.Form["Gender"].ToString();
但它没有工作......
But it didn't work...
推荐答案
像这样放置你的代码:
if (Request.Form["Gender"] != null)
{
string selectedGender = Request.Form["Gender"].ToString();
}
请注意,如果未选择任何 RadioButton,则 Request.Form["Gender"]
将为 null.
Note that Request.Form["Gender"]
will be null if none of the RadioButtons are selected.
查看下面的标记
<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>
对于两个按钮,即 input type="submit"
和通常的 asp:button
,Request.Form["Gender"]
是在 PostBack
上会有一些价值,前提是选择了任一 RadioButtons.
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.
是的,仅在 PostBack
时,即当您点击任一按钮而不是首次加载时.
And yes, upon PostBack
only, i.e. when you hit either of the buttons and not on first load.
这篇关于从 html 单选按钮获取价值 - 在 aspx-c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!