As we all know whenever we build an ASP.NET application with a MasterPage, all the controls receive a prefix such as “ctl00$<ContentPlaceHolderID>$<NameYouGaveToControl>”  to their names according to their place in the page hierarchy. That is done in order to prevent from two controls to have the same ID on the same page.

We encounter a problem whenever we want to seek a control. Lets assume we have a page with a control named “LoginButton”. Normally we would use the following command:


This will work only if we don’t have a MasterPage present. If a master page is present we must add the prefix in order for this to work, so the line will look like this:


That will also work. But we don’t want to see hard coded strings like “ctl00$ContentPlaceHolder1″ because if tomorrow we will change the hierarchy of the page, change names or something like that, everything will fall apart.

There is a nice way to make this more robust and simple to maintain. My solution is to save a variable that holds a reference to the closest ContentPlaceHolder. The rational is that a Page does not need to know its place in the hierarchy of pages in the application, but it should know the closest ContentPlaceHolder above it. We have to create a manager class that will supply the right Place Holder, that way if we change something, we only need to make the adjustments in one place.

Manager will do something like this:

ContentPlaceHolder C =
Application["Page1Holder"] = C;

Of course you can store the reference wherever you see fit (Static Class and so on). When we need to access the control we will do this:

ContentPlaceHolder c = (ContentPlaceHolder)Application["Page1Holder"];
Control b = c.FindControl("LoginButton");

And that’s that. No need to know where we are in the page hierarchy.


Tags :

9 Responses to “Find Controls in an ASP.NET Page with A MasterPAge”

  1. Nikola

    Said on July 4, 2008 :

    I have problem with two type for searching control. I have 5 User Controls i one page, and I try to find label for error message in 1 user control.
    First step:
    Second step:
    In debug mode, I see that everything is ok with contolID, I mean Controls[0] has controlId “ctl00″, and Controls[7] – “lblErrorMain”, but in second step I have error:”System.NullReferenceException: Object reference not set to an instance of an object”.

    Please write me your solution, tnx

  2. Sunil Sarma

    Said on July 4, 2008 :

    Great help for me.
    Thanks you very much.

  3. Amit

    Said on July 4, 2008 :

    @ Nikola

    I will need more information. But my first Idea will be this: I think that the name of the control is not “ctl000″ that is only the prefix of the name it is usually “ctl00_ or something like that

  4. Nikola

    Said on July 4, 2008 :

    Hi Amit,
    I see in debug mode that
    is control with ControlID = “ctl00″ and
    is control with ControlID = “lblErrorMain”.
    I have this code:
    (this.Parent.NamingContainer.Controls[0].TemplateControl.Controls[7] as Label).Text = “…message…”;
    (this.Parent.NamingContainer.FindControl(“ctl00″).TemplateControl.FindControl(“lblErrorMain”) as Label).Text = “…message….”;

    In first step is everythiong ok, but in second crushing.
    Any idea why?

  5. Faheem

    Said on April 21, 2009 :

    In my project I tried for adding a new row on button click and it is working fine, But when I am trying to put that gridview inside
    a contentplaceholder (as I am using a master page).It is not working.It is not taking values from gridview footer text boxes.
    please provide me any solution for this as I am not able to proceed.Below is the code I wrote on save click.

    Dim chk As CheckBox = Grid_Phone.FooterRow.FindControl(“CheckBox1″)

    Dim tx1 As TextBox = Grid_Phone.FooterRow.FindControl(“TextBox1″)

    Dim tx2 As TextBox = Grid_Phone.FooterRow.FindControl(“TextBox2″)

    Dim tx3 As TextBox = Grid_Phone.FooterRow.FindControl(“TextBox3″)

    Dim tx4 As TextBox = Grid_Phone.FooterRow.FindControl(“TextBox4″)

    Dim tx5 As TextBox = Grid_Phone.FooterRow.FindControl(“TextBox5″)

  6. ross

    Said on April 27, 2009 :

    What is the application?
    How can I get the application name?

  7. ross

    Said on April 27, 2009 :

    How can I get the applicatio name?
    If I use the below approach, how can invoke the click event for this button?


  8. ostrov

    Said on December 2, 2009 :

    Thank you,
    very interesting article

  9. J

    Said on February 17, 2011 :

    THANK YOU!!! This was driving me crazy.

Post a Comment