ASP.Net c# Which radio button in a given GroupName is selected?
Asked Answered
D

3

11

I have 30 individual RadioButtons. I can not use a RadioButtonList. There are 3 groups of buttons. Each group has a unique GroupName. Everything works properly in the web browser. How can i tell on a post back which button is selected within each of the given GroupsNames?

EDIT: the function i used

private string getRadioValue(ControlCollection clts, string groupName)
{
    string ret = "";
    foreach (Control ctl in clts)
    {
        if (ctl.Controls.Count != 0)
        {
            if (ret == "")
                ret = getRadioValue(ctl.Controls, groupName);
        }

        if (ctl.ToString() == "System.Web.UI.WebControls.RadioButton")
        {
            RadioButton rb = (RadioButton)ctl;
            if (rb.GroupName == groupName && rb.Checked == true)
                ret = rb.Attributes["Value"];
        }
    }
    return ret;
}
Dibs answered 6/12, 2010 at 23:40 Comment(0)
P
9

You have to check all radiobuttons checked property.
There is no simple way to check it by groupName. (you can write method that scan all radiobuttons in some control container and return list of pairs groupName, control checked, but easier is to scan all rb)

Pull answered 6/12, 2010 at 23:46 Comment(0)
Y
1

Attach the same handler to each RadioButton. Then check the properties you are looking for. Set Enable postback to true.

protected void RadioButton1_30_CheckedChanged(object sender, EventArgs e)
{
    RadioButton rb = (RadioButton)sender;        
    string txtVal = rb.Text;
    string groupName= rb.GroupName;
    int tmpInt;
    Int32.TryParse(txtVal, out tmpInt);

}
Yellowknife answered 12/9, 2012 at 18:45 Comment(1)
This would require a page refresh each time the user changes the radio.Dibs
A
0

You can use Request.Form("groupName")

Accretion answered 7/12, 2010 at 2:11 Comment(4)
This will not work as ASP.NET adds crud to the name attribute. For example: ctl00$cphBody$Mthree The GroupName is Mthree, if I were to use Request.Form I would need to know the crud part as just the GroupName is not accessible.Dibs
Well, if you've got your pages set up in a way that requires mangling, you can typically find out what the crud part is using the ClientID property of one of your controls.Accretion
Is the crud part the same for the ID and the Name? I don't see a ClientName parameter.Dibs
It's either the same, or there are minor differences--like one uses $ where the other uses _ or :, so a string replace can usually do the trick.Accretion

© 2022 - 2024 — McMap. All rights reserved.