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#
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
