9

On one site, I'm only using a single level Masterpage and in a page using that master, I can do this.Master.FindControl("controlName") to access the control. Works fine.

However, using the same code on a site with two masterpage levels. MainMaster and SpecificMaster which has MainMaster as its Master.

So on the page which uses SpecificMaster, FindControl is returning null for the object. The only difference I'm seeing is the nesting of the masterpages.

When I set breakpoint and look at page.Master, it's showing SpecificMaster and SpecificMaster is showing MainMaster as its master correctly, but FindControl is still failing.

When I view source in IE, the control is correctly named, no .NET munging going on.

Any thoughts here?

TIA!

klkitchens
  • 1,202
  • 2
  • 16
  • 39

7 Answers7

21

When you're nesting master pages, you'll get an extra container "Content" you need to look through.

As a result, if you're trying to use FindControl from a given child page the usual approach is something to the effect of:

Label myLabel = (Label)this.Master.FindControl("myLabel");
myLabel.Text = "Success!";

Since we have a nested master page, with "myLabel" in the child master, this control will be contained within a content control.

So, this changes the code to:

ContentPlaceHolder ph = (ContentPlaceHolder)this.Master.Master.FindControl("yourContentPane");

Label myLabel = (Label)ph.FindControl("myLabel");
myLabel.Text = "Success!";

and in VB.NET

Dim ph As ContentPlaceHolder = DirectCast(Me.Master.Master.FindControl("yourContentPane"), ContentPlaceHolder)

Dim myLabel As Label = DirectCast(ph.FindControl("myLabel"), Label)
myLabel.Text = "Success!"

The content from the child page is loaded into the first master page control, which is subsequently loaded into the grandparent master page.

DreamTeK
  • 32,537
  • 27
  • 112
  • 171
Alexis Abril
  • 6,389
  • 6
  • 33
  • 53
  • 1
    This is even more confusing if you have 3 levels of master page files. Basically you can only get to your control by going to the top level masterpage file and drilling down through all the content placeholder controls until you reach what you're looking for. – Landon Poch Sep 24 '12 at 16:31
  • 2
    @LandonPoch, yes that doesn't seem very intuitive to me. – Justin Jan 07 '13 at 17:46
  • 1
    `(ContentPlaceHolder).this` should be `(ContentPlaceHolder)this` No dot `.` – nu everest Feb 13 '14 at 20:55
3

have you tried this.Master.Master.FindControl("controlname"); ?

somacore
  • 6,294
  • 3
  • 24
  • 19
0
HyperLink hl = (HyperLink)Master.Master.FindControl("HyperLink3");

This is the easiest way to find controls from the nested master pages.

tckmn
  • 57,719
  • 27
  • 114
  • 156
Maulik Anand
  • 1,429
  • 3
  • 15
  • 19
0

It is working as well for cross-page postback:

ContentPlaceHolder ph = (ContentPlaceHolder)PreviousPage.Master.FindControl("ContentPlaceHolder");

string txt = ((TextBox)(ph.FindControl("UserTextBox"))).Text;

rk.
  • 140
  • 2
0

My scenario was as follows. Not sure if this setup is the correct one, but it allowed me to have master-submaster page setup, and be able to find control.

MasterPage-> SubMasterPage -> ASPX page

MasterPage:

<asp:ContentPlaceHolder ID="MasterPageContentPlaceHolder" runat="server">
</asp:ContentPlaceHolder>

SubMasterPage:

<asp:Content ID="ModuleMainContent" ContentPlaceHolderID="MasterPageContentPlaceHolder" runat="server">
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>

ASPX.cs:

ContentPlaceHolder MainContent = (ContentPlaceHolder)this.Master.Master.FindControl("MasterPageContentPlaceHolder").FindControl("MainContent");
    TextBox var_type = MainContent.FindControl("air") as TextBox;
Shaakir
  • 464
  • 5
  • 13
0

try this

    string txt = ((TextBox)this.Master.FindControl("ContentIDName").FindControl("TextBox1")).Text;
Marcello B.
  • 4,177
  • 11
  • 45
  • 65
  • 1
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Piotr Labunski Feb 18 '20 at 15:57
0

I usually do this:

(TextBox)this.Master.FindControl("ContentplaceHolder1").FindControl("TextBox1");
etlds
  • 5,810
  • 2
  • 23
  • 30