Home   |  Articles   |  Resources   |  Humor   |  Feedback       

  Login   Register 


Looping Through Controls In ASP.NET web form

Posted by on Thursday, January 01, 2004

Looping through controls in an ASP.Net application is fairly easy using some recursion using the ControlCollection. This article demonstrates how to find controls in a web form

Looping through all the TextBox controls in an ASP.Net web form is a commonly asked question. A first try at writing such a function is to loop through the Controls property of the web page. Controls are usually nested, however, so you may discover your code doesn’t find all of the textboxes on a page. Some of the objects in the Controls property may be parent containers for additional controls, so you’ll need to keep following the Controls property down the hierarchy to find all of the text box controls.

Here is an example snippet:

 

private void Page_Load(object sender, System.EventArgs e)
{
    LoopTextboxes(Page.Controls);
}

private void LoopTextboxes(ControlCollection controlCollection)
{
    foreach(Control control in controlCollection)
    {
        if(control is TextBox)
        {
            ((TextBox)control).Text = "I am a textbox";
        }

        if(control.Controls != null)
        {
            LoopTextboxes(control.Controls);
        }
    }
}

Comments:

Looping through controls
By ? on 9/7/2004
if(control.Controls != null)

Small point, but control.Controls is never null, so code will always iterate here. control.Controls.Count will be 0 if you want to prevent iteration.

Good point
By scott on 3/23/2005
The foreach will halt processing when the controls are empty, but still, no need to call recursively if Controls in non null but empty.

Copyright 2004 OdeToCode.com 


The Blogs
Subscribe to the OdeToCode blogs for the latest news, downloads, new articles, and quirky commentary.
New Articles
A Software Developer's Guide to HTTP Part V– State & Security
An overview of cookies and authentication protocols for HTTP and web programming.

A Software Developer's Guide to HTTP Part IV– Web Architecture
Proxy servers, caching, and the flexibility of the web are the focus for this article as we view HTTP from an architectural perspective.

A Software Developer's Guide to HTTP Part III–Connections
In this article we'll look at HTTP from a networking perspective, and talk about HTTP connection optimizations like persistent connections and parallel connections.

Most Popular Articles
Table Variables In T-SQL
Table variables allow you to store a resultset in SQL Server without the overhead of declaring and cleaning up a temporary table. In this article, we will highlight the features and advantages of the table variable data type.

AppSettings In web.config
In this article we will review a couple of pratices to keep your runtime configuration information flexible.

ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps
MasterPages are a great addition to the ASP.NET 2.0 feature set, but are not without their quirks. This article will highlight the common problems developers face with master pages, and provide tips and tricks to use master pages to their fullest potential.

Contribute Code
Privacy
Consultancy