LogoASPAlliance: Articles, reviews, and samples for .NET Developers
CodeSnip: Find Control in Nested Master Pages
by Abdulla Hussein AbdelHaq
Average Rating: 
Views (Total / Last 10 Days): 46598/ 133


Finding a server control inside a Content page inheriting from nested master pages is one of the problems that so many developers have faced. So in this code snippet, I will show you how to solve this issue.


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.


First of all, we should search about the content place holder of the outer master page, and then search about the content place holder of the inner master page. Finally, we will search deeply inside the two content place holders about our label control.

Listing 4 is the code that you need to write.

Listing 4: Find control inside nested master pages

' Outer Master Page
Dim Outer_CP As ContentPlaceHolder
Outer_CP = TryCast(Me.Master.Master.FindControl("ContentPlaceHolder1"), _
' Inner Master Page
Dim Inner_CP As ContentPlaceHolder
Inner_CP = TryCast(Outer_CP.FindControl("ContentPlaceHolder2"), _
Dim lbl As Label = TryCast(Inner_CP.FindControl("Label1"), Label)
lbl.Text = "Text was changed inside nested master page!"

In this code snippet I have illustrated how to find a control inside nested master pages in .NET 2008. I hope that this article will help you to work with nested master pages easily.

Product Spotlight
Product Spotlight 

©Copyright 1998-2018  |  Page Processed at 2018-06-20 10:34:53 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search