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

Problem

Listing 1: Main Master Page

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

Listing 2: Second Master Page

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

Listing 3: Content Page

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

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

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 4 and 1 and type the answer here:

User Comments

Title: Great logic!   
Name: d-cpt
Date: 8/17/2009 10:45:31 AM
Comment:
Excellent article.
Thanks a lot.
Title: Less Code   
Name: Lannie Lawton
Date: 7/18/2009 3:26:25 PM
Comment:
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: 6/23/2009 2:52:21 AM
Comment:
this is good example to get master page controls
Title: Great   
Name: Abidali
Date: 3/24/2009 8:21:40 AM
Comment:
nice article
Help me lot

Product Spotlight
Product Spotlight 






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


©Copyright 1998-2009 ASPAlliance.com  |  Page Processed at 11/22/2009 1:03:20 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search