Getting value from html radio button - in aspx-c#(从 html 单选按钮获取价值 - 在 aspx-c#)
问题描述
我有以下 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#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 html 单选按钮获取价值 - 在 aspx-c#


基础教程推荐
- JSON.NET 中基于属性的类型解析 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01