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

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>

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

I tried this :

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

But it didn’t work…

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

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.

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

Method 2

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.

It is better to access it like

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

}

Method 3

Use a 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;

Method 4

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.

int rbratebyname = 0;

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x