If you need a little more layout freedom than the RadioButtonList gives you in ASP.NET you have to resort to the normal RadioButton. Unfortunately there is no easy way to afterwards figure out which button has been checked.
Here is a simple recursive Script in C#:
/// <summary> /// Searches recursivly through the controls tree for the /// Checked RadioButton. /// </summary> /// <param name="parent"> /// Control that contains the RadioButtons, can be the Page object. /// </param> /// <param name="groupName"> /// Name of the RadioButton Group /// </param> /// <returns>The RadioButton, null if it is not found</returns> public RadioButton GetCheckedRadioButton( Control parent, string groupName) { foreach (Control ctrl in parent.Controls) { if ((ctrl is RadioButton) && ((RadioButton)ctrl).GroupName.Equals(groupName) && ((RadioButton)ctrl).Checked == true) { return (RadioButton)ctrl; } else if (ctrl.Controls.Count > 0) { Control fromChild; fromChild = GetCheckedRadioButton( ctrl, groupName); if (fromChild != null) return (RadioButton)fromChild; } } return null; }//end GetCheckedRadioButton
A function in VB would be very similar. Good luck!
4 Kommentare zu “Find checked Radio Button in ASP.NET”
Good example and just what I needed. I used it (with modification) to uncheck radio buttons by groupname. Thank you!
Good Stuff! Glad it was of some help.
Thank you for the function. Helped me with my coding as well. I’ll keep it also for future use in my code library.
Thanks for this.