简体   繁体   中英

Best way to find the value of the selected radio button in a group of radio buttons (associated with GroupName attribute) in ASP.NET

I am working on retrieving the value of the selected radio button out of three radio buttons that share the same GroupName attribute. I will need to find this value with the C# code-behind as I am using an MVP pattern in my application and will pass this value from the code-behind to the presenter.

In the .aspx file:

<table>
   <tr>
      <td style="width: 25px">
         <asp:Label ID="rmd" runat="server" Text="*" CssClass="RequiredField" />
      </td>
      <td align="left" style="width: 350px">
         <asp:Label ID="rmd2" runat="server" Text="Raw material dimensions (width, length, thickness)" CssClass="FieldHeader"></asp:Label>
      </td>
      <td valign="bottom">
         <asp:RadioButton ID="rbRawMaterialDimensions" runat="server" TextAlign="Left" CssClass="FieldHeader"
                                GroupName="PlatesManufacturedInHouse" />
      </td>
   </tr>
   <tr>
      <td style="width: 25px">
         <asp:Label ID="hpn" runat="server" Text="*" CssClass="RequiredField" />
      </td>
      <td align="left" style="width: 350px">
         <asp:Label ID="hpn2" runat="server" Text="New HPN Created (main OR raw material item)" CssClass="FieldHeader"></asp:Label>
      </td>
      <td valign="bottom">
         <asp:RadioButton ID="rbNewHPN" runat="server" TextAlign="Left" CssClass="FieldHeader"
                                GroupName="PlatesManufacturedInHouse" />
      </td>
   </tr>
   <tr>
      <td style="width: 25px">
         <asp:Label ID="na" runat="server" Text="*" CssClass="RequiredField" />
      </td>
      <td align="left" style="width: 350px">
         <asp:Label ID="na2" runat="server" Text="Not Applicable" CssClass="FieldHeader"></asp:Label>
      </td>
      <td valign="bottom">
         <asp:RadioButton ID="rbNotApplicable" runat="server" TextAlign="Left" CssClass="FieldHeader"
                                GroupName="PlatesManufacturedInHouse" />
      </td>
   </tr>
   <tr>
         <asp:Label ID="Label24" runat="server" Text="Required Field Missing: An item classification is required."
                                TextAlign="Left" CssClass="Warning" Visible="False" />
   </tr>
</table>

I have tried using the <asp:RadioButtonList> tag, but as I need to include labels for each button (as well as add validation in the future), it seems that using the GroupName attribute is the best solution currently, as far as what I have read online.

I have checked over other StackOverflow questions and answers to find the best way to find the selected radio button and it's associated value, but I can not seem to find the best possible solution in my scenario. The closest scenario to my question is this question I found on here.

I am confused as to what the answer to the question means when it says:

where radioButtonsContainer is the container of the radiobuttons

I created an <asp:Panel ID="PanelPlatesManufacturedInHouse" runat="server" CssClass="outercontainer"> tag surrounding the three radio buttons, but am not sure if this is the proper way to use a container.

I seem to be stuck in my implementation now, if anyone knows the best way to go forward with this, I would appreciate the help :)!

Thanks for reading!

You can give your table a class or ID, and loop all the Radio Buttons in that table to find the value.

So add an ID

<table id="myTable">

Then find the correct value

<script>
    function findRadioValue() {
        $('#myTable input[type=radio]').each(function () {
            if ($(this).prop('checked')) {
                alert($(this).val());
            }
        });
    }
</script>

Update

I somehow though it was jQuery, my bad. But I've created a method to get the correct control ID based on the GroupName. Note that this uses a Master Page. If you do not use one you use foreach (var control in Page.Controls)

public string getRadioButtonIdByGroupName(string groupName)
{
    //loop all controls on the page
    foreach (var control in (Master.FindControl("ContentPlaceHolder1") as ContentPlaceHolder).Controls)
    {
        //check if the control is a radiobutton
        if (control is RadioButton)
        {
            //cast the control to a radiobutton
            var radioButton = control as RadioButton;

            //check if it is the correct group name and if it's checked
            if (radioButton.GroupName == groupName && radioButton.Checked)
            {
                //return the value
                return radioButton.ID;
            }
        }
    }

    return "NotFound";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM