Access master page control from content page


By C-Sharp Guru
Printer Friendly Version
View My Articles

  

Finally..on public demand..Access master page control from content page with all the details!!!



The common method to access master page control (in this example lable) from content page is given below:

    Dim myLabel As Label
    myLabel = CType(Master.FindControl("masterpageLabel"), Label)
    If Not mpLabel Is Nothing Then
        Label1.Text = "Master page label = " + myLabel.Text
    End If

To demonstrate how we create and use these Properties, imagine we have a Master Page which contains a Label control (we'll just call it "Label1"), and we want to show it's value in a TextBox on our Content Page. The Master Page, would look like this:

    <%@ Master Language="VB" CodeFile="site.master.vb" Inherits="site" %>  
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
      
    <html xmlns="http://www.w3.org/1999/xhtml" >  
    <head runat="server">  
        <title>Untitled Page</title>  
    </head>  
    <body>  
        <form id="form1" runat="server">  
       <div>  
           <asp:Label ID="Label1" runat="server" Text="Testing"></asp:Label>  
           <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">  
           </asp:contentplaceholder>  
       </div>  
       </form>  
   </body>  
   </html>  

 Content Page would look like this:

   <%@ Page Language="VB" MasterPageFile="~/site.master" AutoEventWireup="false" CodeFile="Default1.aspx.vb" Inherits="Default1"  %>  
   <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">  
       <asp:TextBox ID="txtTest" runat="server"></asp:TextBox>  
   </asp:Content>  

Now, we need some way of getting the Master Page to expose the Text property of it's "Label1" control. This is where the Properties come in and to set this up we need to make the Property Public so that it can be accessed by other pages. So, our code behind for the Master Page will now consist of:

    Partial Class site  
        Inherits System.Web.UI.MasterPage  
      
        Public Property MyText()  
            Get  
               Return Label1.Text  
            End Get  
            Set(ByVal value)  
                Label1.Text = value  
            End Set  
        End Property  
    End Class  

You'll notice that there is both a Get and a Set method for this Property which will allow us to both retrieve and update the value. To do this from our Content Page, we first need to get a reference to the Master Page, which can be done easily by using the Page.Master property, and then we need to cast the Master Page to the type of Master we are using so that we can see the Public Property and Get the value. We can also use the same methods to Set the property, as is shown by looking at the code behind for the Content Page:

    Partial Class Default1  
        Inherits System.Web.UI.Page  
      
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
            ' Set the value of the Master Page's Label to the Content Page's TextBox  
            txtTest.Text = CType(Page.Master, site).MyText  
            ' Update the Master Page's Label  
            CType(Page.Master, site).MyText = "Changed"  
        End Sub  
    End Class  

When you run this code in your development environment, you'll see that the first Label shows our "Changed" text, whereas the TextBox will show the previous value which we retrieved via the Get method.


Biography
I am System Programmer. I would love to do research oriented works on performance, securities and system programming. I am also interested in LINUX Kernel Programming.

button
 
Article Discussion: Access master page control from content page
C-Sharp Guru posted at 23-Oct-08 01:02
Original Article

promotion
Silverlight    WPF    WCF    WWF    LINQ   
JavaScript    AJAX    ASP.NET    XAML   
C#    VB.NET    VB 6.0    GDI+    IIS    XML   
.NET Generics    Anonymous Methods    Delegate   
Visual Studio .NET    Expression Blend    Virus   
Windows Vista    Windows XP    Windows Update   
Windows 2003 Server    Windows 2008 Server   
SQL Server    Microsoft Excel    Microsoft Word   
SharePoint    BizTalk    Virtual Earth   
.NET Compact Framework    Web Service   

"Everything" RSS / ATOM Feed Parser
How to send and receive messages through message queuing in .Net
How to Read text file as database
SQL Server 2005 Paging Performance Tip
Display code of web page.
Fully Scalable Excel File Importer class for .net using Microsoft Jet driver
Generic Chart Color Manager class that can be used for any charts
Helper class to style the infragistics wingrid
Using Reflection to detemine as Assembly Info in and out.
Helper class to play with Window (Owners and position)
Resolving displayname from the culture using the XmlLanguage and LanguageSpecificStringDictionary class
Manipulate file attributes in VB.NET
Forms Based Authentication Filtered Content Editor for SharePoint
How to create a Tree View of the Windows Folder and extract all the file-folder info.
How to use AssemblyInfo.cs file in win forms to provide much needed information on Assemblies
Sorting In Datagrid
Helper class to work with NativeMethods in the native api's
Silverlight Line Of Business Applications With Offline WPF Versions
C# : Database monitoring system using XML file
C# : Adding ComboBox to ListView SubItem
Sum of Numbers Captcha: Keeping it Simple
C# Create a Piechart for the specified Hard Disk Drive Utilization
Extension Methods for DataSet and DataTable that makes tasks easier
Accessing IIS Hosted WCF Services from PHP
Helper class that provides most commonly used Extension Methods for DateTime object
Helper class to work with a Status Bar in WPF.
Finding Unmatched Records in Dataset Tables Using Linq
Silverlight Toolkit: Autocomplete TextBox Stock Symbols and Chart
COOL Auto Complete textbox using javascript
Creating a Serializable Log Entry for Microsoft Enterprise Library to log to a Database
ASP.NET Searching Values in Datagrid