Brendan Enrick's Blog

Daily Software Development.


Accessing Controls inside of Templated Controls

by Brendan Enrick Friday, October 26 2007 15:03

One question that seems to come up often in the asp.net forums is from people who are trying to access controls within controls using templates. LoginViews and CreateUserWizard controls are two commonly used templated controls. These templated controls don't actually have their contents known until run-time because it is dependant on something else; data from a database, user permissions, etc.

Since this information is not known you can't just access the inner controls as you would normal controls, because it is not known until run-time what the controls are. You will get compiler errors if you try to access them. The easiest way to get the controls you need is to use a recursive find control function. Steve Smith has blogged about Using a Recursive Find Control as well as about a very important Code Optimization for using a recursive find control.

The cool thing about the recursive find control is that it will dig down into the controls collection of a control you specify looking for the control you are looking for. It is great for templated controls, because it is a lot neater than trying to statically go after it like this.

Bad Code Do Not Use This

Label Label1 = LoginView1.Controls[2].Controls[2].Controls[1].Controls[3].FindControl("Label1") as Label;

The problem with the above code is that if you move anything you'll break the code. It is not very stable, and is even hard to tell what is being done. Do not worry there is a better way of handling this. A recursive find control is an expensive operation, and this is why Steve's code optimization is important. You don't want to execute the find control more times than needed.

Good Code Use This

public static System.Web.UI.Control FindControl(System.Web.UI.Control start, string id)
{
    System.Web.UI.Control foundControl;
    if (start != null)
    {
        foundControl = start.FindControl(id);
        if (foundControl != null)
            return foundControl;
        foreach (Control c in start.Controls)
        {
            foundControl = FindControl(c, id);
            if (foundControl != null)
            return foundControl;
        }
    }
    return null;
}

The above code will find controls nested within templated controls for you. This code will probably go in one of your class libraries where you keep utility functions. When you're calling this function, I would recommend you call it within a property as in Steve's optimization. The following code shows how you would do the previous example in a better way.

Good Code Use This

private Label _label1 = null;
private Label Label1
{
    get
    {
        if (_label1 == null)
        {
            _label1 = Utilities.FindControl(LoginView1, "Label1") as Label;
        }
        return _label1 ;
    }
}

This allows you to access the control as you normally would try. You can just type in the name of the property and it will let you use it as if it were the control and there were no templated control there at all. Makes working with LoginViews and CreateUserWizard controls.

Repeating templated controls such as the GridView can also benefit from this functionality, but you need to be more careful with them because there is a control with the name you're looking for in each row of the repeating control. Make sure when using this there that you get the correct row first, and only search within that row.

Have fun finding your controls.

Tagged as:

Comments

2/18/2011 8:03:47 AM #

pingback

Pingback from mmabattle.wordpress.com

Battle of welterweight prospects to headline Matrix Fights IV with Martinez vs. Wing « MMA

mmabattle.wordpress.com

2/18/2011 8:53:00 PM #

pingback

Pingback from mmabattle.wordpress.com

Waylon Lowe, Nik Lentz Booked for UFC Fight Night in Seattle « MMA

mmabattle.wordpress.com

2/19/2011 9:38:43 AM #

pingback

Pingback from mmabattle.wordpress.com

Thank you @otm for this dope matching luggage « MMA

mmabattle.wordpress.com

2/19/2011 11:17:39 AM #

pingback

Pingback from mmabattle.wordpress.com

Lightweights Nik Lentz and Waylon Lowe sign on to UFC Fight Night 24 « MMA

mmabattle.wordpress.com

2/19/2011 12:56:49 PM #

pingback

Pingback from mmabattle.wordpress.com

Jonathan Brookins looking at June date with Jeremy Stephens « MMA

mmabattle.wordpress.com

2/19/2011 4:14:39 PM #

pingback

Pingback from mmabattle.wordpress.com

UFC NEWS: Josh Grispi vs. George Roop on tap for TUF 13 Finale in June « MMA

mmabattle.wordpress.com

2/19/2011 5:54:41 PM #

pingback

Pingback from mmabattle.wordpress.com

I believe in god. « MMA

mmabattle.wordpress.com

2/19/2011 7:33:11 PM #

pingback

Pingback from mmabattle.wordpress.com

Twitter Mailbag: Fedor’s Future, Anderson Silva’s Long Wait and More « MMA

mmabattle.wordpress.com

2/19/2011 9:11:20 PM #

pingback

Pingback from mmabattle.wordpress.com

[Breaking news] Tito pulls out « MMA

mmabattle.wordpress.com

2/19/2011 10:51:12 PM #

pingback

Pingback from mmabattle.wordpress.com

MMA LIVE: Strikeforce “Fedor vs. Silva” review, UFC 129 tickets sales and more on new edition of ESPN2 program « MMA

mmabattle.wordpress.com

2/20/2011 12:29:42 AM #

pingback

Pingback from mmabattle.wordpress.com

NBA Dunk contest tonight. « MMA

mmabattle.wordpress.com

2/20/2011 10:37:05 AM #

pingback

Pingback from mmabattle.wordpress.com

U.S. Marine Chris Davis joins Bellator’s season-four light-heavy tourney « MMA

mmabattle.wordpress.com

2/20/2011 6:42:42 PM #

pingback

Pingback from mmabattle.wordpress.com

PENICK: “Bigfoot” Silva’s unprecedented beating of Fedor Emelianenko a statement to the heavyweight division « MMA

mmabattle.wordpress.com

2/20/2011 9:45:16 PM #

pingback

Pingback from mmabattle.wordpress.com

Nonito Donaire vs Fernando Montiel Shows Why MMA Is Safer Than Boxing « MMA

mmabattle.wordpress.com

2/21/2011 12:47:33 AM #

pingback

Pingback from mmabattle.wordpress.com

Strikeforce: Feijao vs. Dan Henderson promo video « MMA

mmabattle.wordpress.com

2/21/2011 3:48:48 AM #

pingback

Pingback from mmabattle.wordpress.com

Chad Griggs talks to Inside MMA after Strikeforce win – Chad Griggs « MMA

mmabattle.wordpress.com

2/21/2011 9:52:46 AM #

pingback

Pingback from mmabattle.wordpress.com

Who Really Lost at Strikeforce: Fedor vs. Silva? « MMA

mmabattle.wordpress.com

3/10/2011 9:23:51 PM #

pingback

Pingback from darkundereye.com

Dark Under Eye

darkundereye.com

12/23/2011 9:37:56 PM #

pingback

Pingback from atm-lol.net

horoscopes

atm-lol.net

Comments are closed