C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

How to get the Selected Value of all List controls that are present in a Page


Posted Date: 07 Oct 2005    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: Harish RanganathanMember Level: Gold    
Rating: 1 out of 5Points: 10



Introduction


This article describes how to get the selected values of all list controls present in a page using just a single method.

Common Requirement


One of the common requirements we see as required by developers is to create a Questionnaire 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.

Code to Accomplish this task


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);

}

}

}




Summary


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.








Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: 10 Code Snippets for ASP.NET Developers
Previous Resource: A better way of working with IE Webcontrol - TabStrip Control
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use