Welcome to Bilal Haidar [MVP, MCT] Official Blog Sign in | Join | Help

Looping Through Page Controls

A very common question on the ASP.NET forums, is how to loop through the page controls, and find dynamically my control!

Here is the solution, its R E C U R S I O N !!

A very simple example, that loops through the page controls, and once it finds a Panel, it prints out, I am a Panel

 

    public void ShowPanels(Control p_Control)
    {
        try
        {
            // If control found is of type Panel
            if (p_Control.GetType().FullName.Equals("System.Web.UI.WebControls.Panel"))
            {
				Response.Write ("I am a Panel!");
            }

            // Looping through all controls
            foreach (Control _control in p_Control.Controls)
            {
               ShowPanels(_control);
            }
        }
        catch (Exception ex)
        {
            throw new ApplicationException(ex.ToString());
        }
    }


Hope this helps,

Regards

Published Thursday, May 18, 2006 1:41 AM by BilalHaidar [MVP]

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
required 
(required)