If you would like to hire my services, you can now do so by visiting the following link:
Website Design Darlington
Article Statistics
Code Bank Statistics
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.
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: <%@ 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> and our 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.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.
Posted on 12/10/2007 07:17:27
1. Fuquey 18/02/2008 21:02:01
I got this error message - "'MyText' is not a member of 'System.Web.UI.MasterPage'." May I know how to solve?
2. burghboy 26/03/2008 16:23:09
This was very helpful. Thanks. Fuquey - 'MyText' is not a member of the MasterPage class, it's a property that you add to your master page class definition in the .vb or .cs code-behind page. That's why you have to do the type conversion or cast to pick up the members of your master page class ('site') in the example
3. jinal 06/08/2008 06:00:30
i have one master page. In it one label is display . but i want that label visible true in its content page then how can i do this?
Please keep your comments relevant to this page. Any inappropriate or purely promotional comments may be removed. Email addresses are never displayed but are required so you can confirm your comments.