Hi, This article describes how to get the selected values of all list controls present in a page using just a single method. One of the common requirements I see as required by developers is to create a Questionarre which consists of many questions with Radio
Button Options as answers. To loop through all the radiobuttons and get the selected value of them, its tedious if you write code for each and every radiobutton. The following code accomplishes the task. private void Button1_Click(object sender, System.EventArgs
e) { foreach(System.Web.UI.Control ctl in this.Page.Controls) { LoopAllControls(ctl); } } public void LoopAllControls(System.Web.UI.Control oControl) { foreach(System.Web.UI.Control frmControl in oControl.Controls) { if(frmControl.GetType().ToString()=="System.Web.UI.WebControls.RadioButtonList")
{ RadioButtonList rList=new RadioButtonList(); rList=(RadioButtonList)frmControl; Response.Write(rList.SelectedValue); } if(frmControl.HasControls()) { LoopAllControls(frmControl); } } } The above code just Response.Writes the selected values of the radio
button lists. You can have as many radio button lists as you want in the page. The above code would work for the same. However, if you want to find some total or store, you can use a variable to get each value. Hope it helps.
ranganh
Star
12085 Points
2435 Posts
Microsoft
SelectedValue of all List controls that are present in a Page
Nov 17, 2004 11:42 AM|LINK
Harish
http://geekswithblogs.net/ranganh