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

AndreyAkinshin
  • 18,603
  • 29
  • 96
  • 155
Nave Tseva
  • 868
  • 8
  • 24
  • 47

4 Answers4

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.

Manish Mishra
  • 12,163
  • 5
  • 35
  • 59
  • Is it a must to use `Request.Form`? Can't I just call the control by its `id` from server side (C# code behind)? – aspiring Jun 05 '15 at 06:17
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"]))
{

}
V4Vendetta
  • 37,194
  • 9
  • 78
  • 82
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;
Linus Caldwell
  • 10,908
  • 12
  • 46
  • 58
  • Thank you, but I have to do the list in html, there is option to get value when it is in HTML? – Nave Tseva Mar 29 '13 at 09:02
  • @Nave, Why? You use an ASP.net form with `runat="server"`. So why don't use a `RadioButtonList`? – Linus Caldwell Mar 29 '13 at 09:06
  • Hi, I am just learning this subject, and the teacher told us try get value from html radio button... Is it even possible? – Nave Tseva Mar 29 '13 at 09:08
  • 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. – Linus Caldwell Mar 29 '13 at 09:10
  • 3
    @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. – AaA Aug 01 '13 at 02:22
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"]);
}
RamenChef
  • 5,557
  • 11
  • 31
  • 43
Aqib Shehzad
  • 79
  • 1
  • 8