从 html 单选按钮获取值 - 在 aspx-c# 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15700148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:38:39  来源:igfitidea点击:

Getting value from html radio button - in aspx-c#

c#asp.nethtmlradio-buttonradiobuttonlist

提问by Nave Tseva

I have the following HTML source

我有以下 HTML 源代码

<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>

My question is how can I get the selected value to variable in the c# page?

我的问题是如何在 c# 页面中将选定的值设置为变量?

I tried this :

我试过这个:

Gender = Request.Form["Gender"].ToString();

But it didn't work...

但它没有用...

采纳答案by Manish Mishra

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.

请注意,Request.Form["Gender"]如果没有选择任何 RadioButtons ,它将为 null。

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.

对于 ieinput type="submit"和 normal按钮,如果选择了 RadioButtons 中的任何一个asp:buttonRequest.Form["Gender"]都将在 上有一些值PostBack

And yes, upon PostBackonly, i.e. when you hit either of the buttons and not on first load.

是的,PostBack仅在,即当您按下任一按钮而不是首次加载时。

回答by Linus Caldwell

Use a RadioButtonList

用一个 RadioButtonList

<asp:RadioButtonList id="RadioButtonList1" runat="server">
    <asp:ListItem value="male">male</asp:ListItem>
    <asp:ListItem value="female">female</asp:ListItem>
</asp:RadioButtonList>

and get the value with

并获得价值

RadioButtonList1.SelectedValue;

回答by V4Vendetta

To start with you will need the form posted the Form collection won't have anything on the page load, so suppose you have a button and you click to submit the form then in the click event handler you can get the selected value with the code you have tried.

首先,您需要发布表单,表单集合在页面加载时不会有任何内容,因此假设您有一个按钮并单击以提交表单,然后在单击事件处理程序中,您可以使用代码获取所选值你试过了。

I guess the collection is null hence the NullReference exception when you access it.

我猜该集合为空,因此访问它时会出现 NullReference 异常。

It is better to access it like

最好像这样访问它

if(!string.IsNullOrEmpty(Request.Form["Gender"]))
{

}

回答by Aqib Shehzad

if you are working with asp.net make sure that HTML control name by Request.Form contains these ct100$ with the name or id through which you are assessing. check the below example.

如果您正在使用 asp.net,请确保 Request.Form 的 HTML 控件名称包含这些 ct100$ 以及您正在评估的名称或 ID。检查下面的例子。

int rbratebyname = 0;

if (Request.Form["ctl00$ContentPlaceHolder1$rate"] != null)
{
    rbratebyname = int.Parse(Request.Form["ctl00$ContentPlaceHolder1$rate"]);
}