|
Create Master Pages in ASP.NET 2.0 (Continued)
Gain Access to Controls
One way to get access to a control in a master page is through the use of the FindControl method. Assuming that there's a Textbox control on the master page called txtSummary, you could reference the textbox like this:
Dim txtSummary As TextBox = _
CType(Master.FindControl("txtSummary"), _
TextBox)
You can also expose controls in the master page through public properties in the class of the master page itself. This method requires you to create a public property inside the master page's code-behind file for each control you want to access. This public property is in the class that contains the actual control you're providing access to, so you cannot give it the same name. A common technique is to use an underscore as a prefix for the property name so that it looks as close as possible to the original control name. This method is the fastest (due to the overhead of the FindControl method), but it can also be confusing and cumbersome. (It also requires additional code for each new control added, just in case it might be accessed from a content page). You'll need to look closely at the pros and cons of each method to select which one is best for you.
You might be wondering about the order of event firing, because both a master and content page can have Page_Load (and other) common events. ASP.NET 2.0 fires page events is this order:
<Content page events>
<Bottom-level master page events>
…
<Top-level master page events>
In the sample project provided with this article, I created a sample master page called SampleMasterPage.master with a textbox control at the top. Here's the code-behind file:
Partial Class SampleMasterPage_master
Sub Page_Load (ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.Load
txtEventLog.Text += vbCrLf
txtEventLog.Text +=
"Master Page_Load Event Handler Fired"
End Sub
End Class
I then created a content page linked to this master page called EventTester.aspx (see Listing 4 for the code-behind files for this content page).
As you can see, each class module has a Page_Load event that writes to the textbox defined in the master page when it gets fired. In addition, EventTester.aspx writes an entry to the textbox control when the user clicks on the Press Me button. You can see the order of event firing by running this sample Web application:
Content page Page_Load
Master page Page_Load
Content page "Press Me" Button.Click
As you can see, the inclusion of master pages in ASP.NET 2.0 provides you with another time-saving tool to help develop consistent Web applications without the additional overhead of include files, user controls, or third-party add-ins. Master and content pages are easy to implement and help you provide consistent content without having to worry about the details. By integrating master pages with new personalization and membership features included in ASP.NET, you can easily add the ability to allow your users to customize their own look and feel by selecting from a set of customized master pages that you provide to them.
About the Author
Doug Thews is the director of software development for D&D Consulting Services in Dallas. Doug has more than 19 years of software-development experience in C/C++, VB/ASP, and VB.NET/C#. He writes the Getting Started column for Visual Studio Magazine, and coauthored the book, Professional ASP.NET Performance (Wrox/Wiley Press). Reach Doug at .
Back to top
|