Get the selected switch is not listed in ASP.NET.

I have several switches belonging to the group. I do not have them in the list, since they are all scattered across the page. How can I easily get the selected switch?

+5
source share
2 answers

This may not be the fastest way, but something like this should work:

private RadioButton GetSelectedRadioButton(string groupName)
{
    return GetSelectedRadioButton(Controls, groupName);
}

private RadioButton GetSelectedRadioButton(ControlCollection controls, string groupName)
{
    RadioButton retval = null;

    if (controls != null)
    {
        foreach (Control control in controls)
        {
            if (control is RadioButton)
            {
                RadioButton radioButton = (RadioButton) control;

                if (radioButton.GroupName == groupName && radioButton.Checked)
                {
                    retval = radioButton;
                    break;
                }
            }

            if (retval == null)
            {
                retval = GetSelectedRadioButton(control.Controls, groupName);
            }
        }
    }

    return retval;
}
+9
source

Use the "GroupName" attribute to group switches into a group. This will lead them to behave as a group. You still have to request them separately for a verified state.

0
source

All Articles