Find checked Radio Button in ASP.NET

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!




Ähnliche Beiträge


4 Kommentare zu “Find checked Radio Button in ASP.NET”



  • Bruce am 12. December 2008 6:52 pm Uhr

    Good example and just what I needed. I used it (with modification) to uncheck radio buttons by groupname. Thank you!


  • Remy Blaettler am 14. December 2008 2:15 pm Uhr

    Good Stuff! Glad it was of some help.


  • Serghei am 26. March 2009 1:25 pm Uhr

    Thank you for the function. Helped me with my coding as well. I’ll keep it also for future use in my code library.


  • Jan Wikholm am 15. April 2009 9:33 am Uhr

    Thanks for this.


Leave a Reply

Your email address will not be published. Required fields are marked *



*