1

I am having an issue in getting the value of an input type = radio In c# My form is in the master page as follows:

   <form id="form1" runat="server">
    <asp:ContentPlaceHolder id="head" runat="server">
 </asp:ContentPlaceHolder>
<div class="header">
    <asp:Label ID="DoctorName" runat="server" Text=""     CssClass="Label"></asp:Label>
    <asp:ImageButton ID="logout" ImageUrl="~/Logout.png" style ="float:right; margin-right:20px;" runat="server" Height="40" Width="30"/>
</div>
        <div>
     <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

      </asp:ContentPlaceHolder>
     </div>  
   </form>

In my child page (c# file) I just tried to do the Request.form["MouvementYesNo"] but it returned a null value what should I do as another method ? should the two radio buttons have the same id? or how to get them by name?

 <li>
        <label>Troubles du mouvement <span class="required">*</span></label>
     <input type="radio" runat="server" name="MouvementYesNo" value="Yes" checked> Yes
    <input type="radio" runat="server" name="MouvementYesNo" value="No"> No


    </li>

1 Answers1

1

Since all of HTML radio buttons have runat="server" attribute which means they're accessible from code behind, put different control IDs and use if condition to check which radio button is selected.

Markup

<input id="RadioButton1" type="radio" runat="server" name="MouvementYesNo" value="Yes" checked> Yes
<input id="RadioButton2" type="radio" runat="server" name="MouvementYesNo" value="No"> No

Code behind

protected void Button1_Click(object sender, EventArgs e)
{
    if (RadioButton1.Checked == true)
    {
        // do something
    }
    else if (RadioButton2.Checked == true)
    {
        // do something else
    }

    // other stuff
}

Note that if you want to use Request.Form, the HTML server control attribute (i.e. runat="server") should be removed, also Request.Form contains null value if no radio button input elements are selected.

Side note:

Better to use RadioButtonList server control like this:

<asp:RadioButtonList ID="MouvementYesNo" runat="server">
    <asp:ListItem value="Yes">Yes</asp:ListItem>
    <asp:ListItem value="No">No</asp:ListItem>
</asp:RadioButtonList>

And you can get currently selected radio button value later:

var selected = MouvementYesNo.SelectedValue;

Similar issue:

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

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
  • Okayy thank you but how to set the radio the circle of theradio button and the text after the circle and a text box on the same line? – BoutrosDouaihy Apr 16 '18 at 04:09
  • You can wrap radio buttons into the label or setting CSS with `float: left;` and/or `display: block;` to adjust the alignment so that radio buttons and text label are both inline (consider see this post: https://stackoverflow.com/questions/2306117/radio-buttons-and-label-to-display-in-same-line). – Tetsuya Yamamoto Apr 16 '18 at 04:13
  • Sorry i mean the radio button list how to set them all in the same line and after them a text box – BoutrosDouaihy Apr 16 '18 at 04:16
  • 1
    I think it's a layout issue which better to be visualized in a new question with current markup & supporting images rather than discussing in comments (remember to mark this question if this issue was solved). The key is manipulating CSS selectors related to radio buttons and text boxes. – Tetsuya Yamamoto Apr 16 '18 at 04:26