Getting value from html radio button - in aspx-c#
Asked Answered
F

4

14

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

Fecteau answered 29/3, 2013 at 8:56 Comment(4)
Didn't work ? Can you expand on that ...Example
Yes, when it came to this line I got this error message: System.NullReferenceExceptionFecteau
Well where are you writing this piece of code and don't you have any button to submit this ?Example
have you checked Request.ParamsMartres
M
25

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.

Moorehead answered 29/3, 2013 at 9:9 Comment(1)
Is it a must to use Request.Form? Can't I just call the control by its id from server side (C# code behind)?Longrange
E
4

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"]))
{

}
Example answered 29/3, 2013 at 9:11 Comment(0)
E
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;
Elongation answered 29/3, 2013 at 9:0 Comment(5)
Thank you, but I have to do the list in html, there is option to get value when it is in HTML?Fecteau
@Nave, Why? You use an ASP.net form with runat="server". So why don't use a RadioButtonList?Elongation
Hi, I am just learning this subject, and the teacher told us try get value from html radio button... Is it even possible?Fecteau
Well, that's not clearly declared. The result of an ASP.net RadioButtonList is clean HTML too. So I would use it in your situation.Elongation
@LinusCaldwell, There are cases that you need to generate a list of radiobuttons based on the entries from the user (which for speed matters generated on client side) and in my case they are textboxes. what OP and me looking for is the way to read this list after it is final in client side.in these cases using asp.net controls is not an option because of massive network traffic they will produce.Carob
C
0

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"]);
}
Cocoa answered 18/11, 2016 at 17:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.