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#
基础教程推荐
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
