Posts
259
Comments
464
Trackbacks
17
Master Pages 101: Using VB 2005 to Access Master Page ASP.NET Controls From a Content Page

One of the best features added to ASP.NET 2.0 was the inclusion of Master Pages. Master Pages provide a template to base web forms on. A Master Page can include HTML elements and ASP.NET 2.0 controls. There may be times when you want to access one or more of the ASP.NET 2.0 controls in a Master Page from a content page created from it. There are two approaches, a strongly typed approach and a not strongly typed approach.

Before we can go any further you need  to learn a little about the Master object. The Master object provides a programatic reference to the Master Page from a content page.

For example, this content page LoadComplete event handler will programmatically assign a value to the page title.

Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
Master.Page.Title = "This page generated on: " + DateTime.Now.ToString End Sub

Now that you know a little about the Master object we can move on to referencing master page ASP.NET 2.0 controls. I'll start with the not strongly typed approach. The following line, used in a content page's Page_LoadComplete event handler for example, will assign the text for a label control (lblDateTime) located on the content page with the text from a TextBox (txtDateTime) control located on the Master Page.

Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
Me.lblDateTime.Text = CType(Master.FindControl("txtDateTime"),TextBox).Text End Sub

The strongly typed approach uses a master page property to provide a reference the master page control.

Partial Class MasterPage
Inherits System.Web.UI.MasterPage
Public Property DateTextBox() As TextBox
Get
Return Me.txtDateTime
End Get
Set(ByVal value As TextBox)
txtDateTime = value
End Set
End Property
End Class

With this property in place on the master page, referencing the txtDateTime textbox from the content page is done simply by accessing the DateTextBox master page property.

Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
Me.lblDateTime.Text = Master.DateTextBox.Text

Me.lblDateTime.Text = CType(Master.FindControl("txtDateTime"),TextBox).Text End Sub

The code above shows both methods being demonstrated.  As with most things in .NET, there is more that one way to accomplish the task. Which approach is better? Although it takes a bit more work to initially set up, I like the strongly typed, master page property approach because it works very well with intelisense.

posted on Monday, June 4, 2007 9:50 AM Print
Comments
Gravatar
# re: Master Pages 101: Using VB 2005 to Access Master Page ASP.NET Controls From a Content Page
courtney
6/6/2007 12:37 PM
what about setting from the content page? i have a masterpage property just like you do above and i need to set the value from the content page.

In my content Page_Load function I do this:

Dim myMaster As MasterPage = CType(Master, MasterPage)
myMaster.SectionIDValue(1)

But I get the underline under "MyMaster.SectionIDValue" saying that it is not a member of System.Web.UI.MasterPage

Is something else required to do this?
Gravatar
# re: Master Pages 101: Using VB 2005 to Access Master Page ASP.NET Controls From a Content Page
Jim Duffy
6/6/2007 3:27 PM
Maybe I'm missing something here. Why are you creating an object, myMaster, that references the master page? Why not just use the Master object to reference items on the current master page?
Gravatar
# re: Master Pages 101: Using VB 2005 to Access Master Page ASP.NET Controls From a Content Page
peters2521
7/19/2007 8:38 AM
putting
<%@ MasterType VirtualPath = "~/yourmasterpagename.master"%>
in the asp content page worked for me.
Gravatar
# re: Master Pages 101: Using VB 2005 to Access Master Page ASP.NET Controls From a Content Page
Umar
1/10/2009 3:06 AM
Exectly what i was looking for....
Gravatar
# re: Master Pages 101: Using VB 2005 to Access Master Page ASP.NET Controls From a Content Page
raghav
1/11/2009 11:44 AM
That really worked well.... thanks
Comments have been closed on this topic.