Fun Infused Games  |   Smooth Operator  |   Evil Scale  |   Starcraft Live  |   Wellplayed.net RSS Feed Available RSS 

  Home   |    Archive   |    Subscribe   |    Search   |    About

Posts prior to 8/2/2010 may be missing data. If you need one of those posts, please contact kriswd40@yahoo.com and I will try and recover/find it.

Loop Through All Controls on an ASP.net Webpage
Date 10/9/2008    Tags ASP.net    (10)

If you're like me, you have on occasion had a need to loop through all the controls on your page. Sadly there is no simple ForEach loop that will take you through all the controls on the page (that would be just too easy). But there is a set hierarchy and by going through that hierarchy, you can get them all.

The basicic heirachy of controls on a page (which uses MasterPages) is MasterPage then HtmlForm then ContentPlaceHolder and then finally will come all the labels, textboxes, and whatever other controls you seek (note that if these controls contain children of their own, like a GridView, you will need to loop through that control to get all the included controls in it).

In order to loop through all of these and get to the controls you really care about, you need to first go through each collection of controls and then when you hit a control of the next type down in the hierarchy, loop through all controls in that particular control. The code below will do just this.
foreach (Control masterControl in Page.Controls)
{
    if (masterControl is MasterPage)
    {
        foreach (Control formControl in masterControl.Controls)
        {
            if (formControl is System.Web.UI.HtmlControls.HtmlForm)
            {
                foreach (Control contentControl in formControl.Controls)
                {
                    if (contentControl is ContentPlaceHolder)
                    {
                        foreach (Control childControl in contentControl.Controls)
                        {

                        }
                    }
                }
            }
        }
    }
}
If you want to get a specific control, for example you'd like to set all your labels to a new message, you simply need to check what type of control it is and if it happens to be a label control, cast a new label as that control and set your text. The following code will set all labels on your page to the text "You found me!". You will need to place this code in the inner most ForEach loop.
if (childControl is Label)
{
    Label newLabel = (Label)childControl;
    newLabel.Text = "You found me!"
}
Alternatively to this method, you could create a function that calls itself recursively and drills down through each control and their children. That would have advantages over the method described above as you would be guaranteed to hit all items on the page (as I mentioned earlier, this code won't get controls inside objects like GridViews). If that's not a requirement of yours though (or you fear recursion as many young programmers do), this method seems a bit easier to follow than recursive calls and won't unnecessarily go through controls you don't care about.

If you're looking for a client-side approach to accessing your controls, check out my article Enable/Disable All Buttons in a GridView using JavaScript, which could be easily modified to get not just all controls in a GridView but all controls on the page.

kick it on DotNetK
                    
                    <br />
                    <br />
                    
                    <center>
                    
                    <A href=Wellplayed.net
This article has been view 349 times.

Advertisement:

Comments

NewToDotNet

Avatar

2/19/2009 4:11:12 PM

Thank you, very well explained.h="0"
Nimesh

Avatar

4/1/2009 1:43:09 AM

Nice article.
It solved few of my problems.

How do we get the control using it's ID property, like getElementByID in javascript?

Or if we can use something like this;
(Label & i).Value = "abc"
where i goes from 1 to 5."0" st
Vu Doan

Avatar

4/9/2009 1:01:28 AM

Thank you very much."0"
Tom Riddle

Avatar

5/26/2009 2:25:36 AM

Thanks a lot.
help me solved my problem :D?sid=
Mathieu Cupryk

Avatar

6/3/2009 6:47:27 PM

It does not work for me.

mathieu_cupryk@hotmail.com
Steve Kaschimer

Avatar

6/18/2009 10:08:13 AM

You are right, more than one way to do this. I coded up the recursive method a couple of days ago and just posted to my blog:

http://www.dashtechnical.com/blogn.ru/t
AL

Avatar

12/15/2009 2:38:20 PM

Very Helpful. Thank You.t
Damage

Avatar

12/16/2009 10:37:15 AM

The recursive approach is a much better way of doing this...

Pseudocode:

FindControl(control parent, string id)
{
if parent.id == id return parent

loop through each control in parent.controls
child = FindControl(control, id)
if child != null return child

return null
}<i
Damage

Avatar

12/16/2009 10:40:51 AM

Sorry, the naming I used might be confusing, here:

FindControl(control parent, string id)
{
if parent.id == id return parent

loop through each child in parent.controls
control = FindControl(child, id)
if control != null return control

return null
}go
crazy

Avatar

8/4/2010 7:22:54 AM

her


Add Comments

Current disabled. Check back soon!
top
top


top
Tags
ASP.net (17)  Annoyances (4)  Video Games (6)  Sage-Like Advice (12)  Domain Name (1)  Internet (5)  NFL (2)  Writing (1)  Visual Studio (1)  Hypership (12)  Site News (2)  Xbox (1)  C# (15)  Sage-Like Advise (1)  Education (1)  Tech Support (1)  MSSQL (1)  Absurd (1)  Abduction Action! (27)  Nasty (36)  Economy (1)  Cool (2)  Sports (11)  .Net (1)  Web Tools (2)  Abduction! (1)  Rant (40)  XBLIG (20)  Tutorial (2)  Nastier (1)  Books (1)  Realty (1)  Programming (3)  Weird (1)  Vista (1)  Development (14)  Design (3)  XNA (35)  Links (1)  Security (2)  JavaScript (7)  CSS (1)  Video (2)  Web Development (1)  Computing (1)  Abudction Action! (1)  Popularity (1)  Game Design (4)  Google (1)  AJAX (4)  Game Development (8)  
top

top
top


Twitter

    Follow me on Twitter



    Buy My Games:
    Abduction Action!
    Nasty

    I'm with Coco.