How can I find the selected RadioButton's value in ASP.NET?
Asked Answered
P

2

16

I have two asp:RadioButton controls which are having the same GroupName which essentially makes them mutually exclusive.

My markup:

<asp:RadioButton ID="OneJobPerMonthRadio" runat="server" 
        CssClass="regtype"
        GroupName="RegistrationType"
        ToolTip="125"/>
<asp:RadioButton ID="TwoJobsPerMonthRadio" runat="server" 
        CssClass="regtype"
        GroupName="RegistrationType"
        ToolTip="200"/>

My intention was to find the tooltip / text of the RadioButton that is checked. I have this code-behind:

int registrationTypeAmount = 0;
if (OneJobPerMonthRadio.Checked)
{
    registrationTypeAmount = Convert.ToInt32(OneJobPerMonthRadio.ToolTip);
}
if (TwoJobsPerMonthRadio.Checked)
{
    registrationTypeAmount = Convert.ToInt32(TwoJobsPerMonthRadio.ToolTip);
}

I find that code ugly and redundant. (What if I have 20 checkboxes?)

Is there a method that would get the checked RadioButton from a set of RadioButtons with the same GroupName? And if not, what are the pointers on writing one?

P.S: I cannot use a RadioButtonList in this scenario.

Phidippides answered 20/4, 2011 at 15:7 Comment(0)
M
18

You want to do this:

RadioButton selRB = radioButtonsContainer.Controls.OfType<RadioButton>().FirstOrDefault(rb => rb.Checked);
if(selRB != null)
{
    int registrationTypeAmount = Convert.ToInt32(selRB.ToolTip);
    string cbText = selRB.Text;
}

where radioButtonsContainer is the container of the radiobuttons.

Update

If you want to ensure you get RadioButtons with the same group, you have 2 options:

  • Get them in separate containers

  • Add the group filter to the lamdba expression, so it looks like this:

    rb => rb.Checked && rb.GroupName == "YourGroup"

Update 2

Modified the code to make it a little more fail proof by ensuring it won't fail if there's no RadioButton selected.

Marven answered 20/4, 2011 at 15:13 Comment(6)
+1: very good thought. I wont have a asp.net(runat="server") container control everytime though.Phidippides
If you don't have a declared container, use the page and find by group as suggested.Marven
@Adraian: not so well. accessing the pages controlcollection should be recursive. actually, I was thinking of accessing the controls by groupname. but webforms architecture is too rigid to do that. loved webforms once, but I guess I should switch to MVC. these days, i'm loving webforms a lot less and loving c# a lot more...Phidippides
Why don't you put the radiobuttons inside a Panel? That would be your radioButtonsContainer.Marven
My radio buttons are inside <asp:Content ID="Main" ContentPlaceHolderID="cphMain" ClientIDMode="Static" runat="server"> but I can't seem to access that container via Page.FindControl. Any more information on this?Bader
I'm not sure whether asp:Content is a control. Try placing you radio buttons inside a asp:Panel, which would be inside your asp:Content. That will surely work.Marven
B
1

You may try writing down a similar method to the one below:

    private RadioButton GetSelectedRadioButton(params RadioButton[] radioButtonGroup)
    {
        // Go through all the RadioButton controls that you passed to the method
        for (int i = 0; i < radioButtonGroup.Length; i++)
        {
            // If the current RadioButton control is checked,
            if (radioButtonGroup[i].Checked)
            {
                // return it
                return radioButtonGroup[i];
            }
        }

        // If none of the RadioButton controls is checked, return NULL
        return null;
    }

Then, you can call the method like this:

RadioButton selectedRadio = 
             GetSelectedRadioButton(OneJobPerMonthRadio, TwoJobsPerMonthRadio);

It will return the selected one (if there is) and it will work for no matter how many radio buttons you have. You can rewrite the method, so that it returns the SelectedValue, if you wish.

Barbarize answered 4/12, 2013 at 14:9 Comment(1)
I used this with "System.Web.UI.HtmlControls.HtmlInputRadioButton" instead of just "RadioButton" in my project and it worked beautifully. Thank you!Errata

© 2022 - 2024 — McMap. All rights reserved.