CodeSnip: Find Control in Nested Master Pages
page 2 of 4
by Abdulla Hussein AbdelHaq
Average Rating: 
Views (Total / Last 10 Days): 28313/ 76


Listing 1: Main Master Page

<%@ Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage" %>
<html xmlns="" >
<head id="Head1" runat="server">
    <title>Outer Master Page</title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    <form id="form1" runat="server">
        <h3>Outer Master Page</h3>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

Listing 2: Second Master Page

<%@ Master Language="VB" CodeFile="MasterPage2.master.vb" 
Inherits="MasterPage2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" 
<h3>Inner Master Page</h3>
<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat=server>

Listing 3: Content Page

<%@ Page Language="VB" MasterPageFile="~/MasterPage2.master" 
CodeFile="Default.aspx.vb" Inherits="_Default" %>
<asp:Content ID="Content1" runat="server" 
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" />

As shown in the previous code, we have a label control inside the content page and that content page is related to the Second Master Page, which is related to the Main Master Page. This will integrate the label control in the nested master page, so that makes finding the label more difficult.

Now we would like to change the text of the label from code behind when we press on the button.

View Entire Article

User Comments

Title: Excellent Example   
Name: Debbie
Date: 2012-04-24 12:49:56 PM
This code was exactly what I was looking for. I have been working on this problem for 2 hrs. Thanks so much!!
Title: Nice!   
Name: mon
Date: 2011-07-07 11:57:13 PM
helped me a lot. been figuring this out for 2 hours.
Title: Developer   
Name: Azhar rizvi
Date: 2010-07-09 2:54:52 AM
Thanks a lot man..
You have done a great job.
keep posting.

Wish u smiles.
Title: developer   
Name: Cas
Date: 2010-04-26 10:50:48 AM
Thanks a bunch. I have been really struggling calling controls in nested master pages, this really simplified it.

Good Stuff :)
Title: developer   
Name: netslaves
Date: 2010-03-09 8:27:32 AM
Title: Great logic!   
Name: d-cpt
Date: 2009-08-17 10:45:31 AM
Excellent article.
Thanks a lot.
Title: Less Code   
Name: Lannie Lawton
Date: 2009-07-18 3:26:25 PM
Dim lbl As label = Me.Master.Master.FindControl("ContentPlaceHolder1$ContentPlaceHolder1$Label1")

Would work too, at least in VS 2008.
Title: good explaination   
Name: soma sekhar
Date: 2009-06-23 2:52:21 AM
this is good example to get master page controls
Title: Great   
Name: Abidali
Date: 2009-03-24 8:21:40 AM
nice article
Help me lot

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-05-29 12:12:21 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search