Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

ASP.NET: Access Master Page controls from the Content Page

From Wiki

Jump to: navigation, search

Summary: An example of how you can get or set the values of a control on the Master Page from one of your content pages.

Need help with ASP.NET? Come and ask a question in our ASP.NET Forum

One of the questions that has been cropping up on the forums a lot recently, is how to interact with with controls on the master page from one of your Content Pages.

There are a couple of ways I can think of to do this, but the method I prefer is to use Properties. 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. Our Master Page, would look like this:

  1. <%@ Master Language="VB" CodeFile="site.master.vb" Inherits="site" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3.  
  4. <html xmlns="http://www.w3.org/1999/xhtml" >  
  5. <head runat="server">  
  6.     <title>Untitled Page</title>  
  7. </head>  
  8. <body>  
  9.     <form id="form1" runat="server">  
  10.     <div>  
  11.         <asp:Label ID="Label1" runat="server" Text="Testing"></asp:Label>  
  12.         <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">  
  13.         </asp:contentplaceholder>  
  14.     </div>  
  15.     </form>  
  16. </body>  
  17. </html>

and our Content Page would look like this:

  1. <%@ Page Language="VB" MasterPageFile="~/site.master" AutoEventWireup="false" CodeFile="Default1.aspx.vb" Inherits="Default1"  %>  
  2. <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">  
  3.     <asp:TextBox ID="txtTest" runat="server"></asp:TextBox>  
  4. </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:

  1. Partial Class site
  2.     Inherits System.Web.UI.MasterPage
  3.  
  4.     Public Property MyText()
  5.         Get
  6.             Return Label1.Text
  7.         End Get
  8.         Set(ByVal value)
  9.             Label1.Text = value
  10.         End Set
  11.     End Property
  12. 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:

  1. Partial Class Default1
  2.     Inherits System.Web.UI.Page
  3.  
  4.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  5.         ' Set the value of the Master Page's Label to the Content Page's TextBox  
  6.         txtTest.Text = CType(Page.Master, site).MyText
  7.         ' Update the Master Page's Label  
  8.         CType(Page.Master, site).MyText = "Changed"
  9.     End Sub
  10. 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.

This article shows you a simple example of how to interact with controls on the Master Page, and will give you the necessary logic to build on this code and apply it to similar situations where you need the interaction between the two pages.

This Hack is part of the ASP.NET Hacks collection

433 Rating: 2.9/5 (110 votes cast)