Monday, June 18, 2007

While working with ASP.NET, from time to time, I need to change some parameter that is common to all controls of on a page. An example of such operation might be setting ReadOnly property for all TextBox controls etc.

Below is a sample method code I use to get a flattened hierarchy of controls on a page:

public static Control[] FlattenHierachy(Control root)
{
    List<Control> list = new List<Control>();
    list.Add(root);
    if (root.HasControls())
    {
        foreach (Control control in root.Controls)
        {
            list.AddRange(FlattenHierachy(control));
        }
    }
    return list.ToArray();
}

As you can see, it is pretty simple. The method recursively traverses control hierarchy and collects each control it finds on its way to a collection which is finally returned to the caller in a form of an array.

As mentioned above we can use this method to set the ReadOnly property of all TextBox-es on the page. To do this, a code similar to the one below can be used:

private void MakeTextBoxesReadOnly()
{
    Control[] allControls = FlattenHierachy(Page);
    foreach (Control control in allControls)
    {
        TextBox textBox = control as TextBox;
        if (textBox != null)
        {
            textBox.ReadOnly = true;
        }
    }
}

Using a combination of the above outlined methods it is easy to implement a kind of ReadOnlyMode page filter. Just identify what controls need to be ReadOnly and how to make them so (RadioButton for example doesn't have a ReadOnly property). When you know how and what you want to make ReadOnly, just override Page's OnPreRender method by adding a call a MakeTextBoxesReadOnly method (or whatever method you create).

Of course all of this is possible thanks to the great ASP.NET control model which is one of the things that I really like about ASP.NET.

kick it on DotNetKicks.com

Monday, June 18, 2007 4:00:20 PM (Central European Standard Time, UTC+01:00)
I wrote a similiar method some time back which I use to grab all of the controls of a givent type on the page. I blogged about it here: http://blog.spontaneouspublicity.com/2006/09/20/how-to-traverse-the-control-tree-in-aspnet-with-c-20/
Tuesday, June 19, 2007 10:00:40 AM (Central European Standard Time, UTC+01:00)
Your solution works and is fine if you need a list of the controls. The drawback is that you're creating the list in memory.
I usually use iterators, which work just fine with foreach loops.
alien
Saturday, August 18, 2007 1:34:47 PM (Central European Standard Time, UTC+01:00)
Here's the .NET 1.1 version of the code for people like me who haven't yet migrated....

public ArrayList FlattenHierachy(Control root)
{
Arraylist alControls = new ArrayList();
list.Add(root);
if (root.HasControls())
{
foreach (Control control in root.Controls)
{
list.AddRange(FlattenHierachy(control));
}
}
return alControls;
}
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview