Nested Masterpages and .FindControl
Asked Answered
R

7

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!

Revolution answered 16/10, 2009 at 13:53 Comment(2)
Where is the control you are looking for? Is on Specific Master, or MainMaster?Silas
SpecificMaster has the control.Revolution
P
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.

Pentomic answered 16/10, 2009 at 14:36 Comment(3)
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.Elohim
@LandonPoch, yes that doesn't seem very intuitive to me.Celik
(ContentPlaceHolder).this should be (ContentPlaceHolder)this No dot .Friesland
A
3

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

Aldin answered 16/10, 2009 at 14:1 Comment(2)
Oh, I thought the main master had the control.Aldin
@Aldin - It says his control is in specificMaster, which is the one between the child page and the top level master.Vd
K
0

It is working as well for cross-page postback:

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

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

Knowitall answered 29/4, 2010 at 13:50 Comment(0)
S
0

I usually do this:

(TextBox)this.Master.FindControl("ContentplaceHolder1").FindControl("TextBox1");
Selmaselman answered 25/7, 2011 at 16:24 Comment(0)
Y
0
HyperLink hl = (HyperLink)Master.Master.FindControl("HyperLink3");

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

Yahrzeit answered 7/2, 2013 at 2:50 Comment(0)
C
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;
Carmina answered 9/3, 2016 at 11:50 Comment(0)
U
0

try this

    string txt = ((TextBox)this.Master.FindControl("ContentIDName").FindControl("TextBox1")).Text;
Unbelt answered 18/2, 2020 at 15:26 Comment(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.Whitson

© 2022 - 2024 — McMap. All rights reserved.