2005-08-18
| Table of Contents: |
| Rate This Article: | Add This Article To: |
( Page 2 of 4 )
In the previous installment of this series, Working with Master Pages and Themes in ASP.NET 2.0, you discovered the benefits that master pages can provide. Master pages lend consistency to the Web site and reduce the work that each developer must perform. They also can also make it easier for non-programmers to add essential content to Web sites. In short, a master page provides a form of automation.
However, master pages can do a lot more than simply automate tasks. By adding classes to a master page, you can also reduce the amount of coding that an individual developer must perform and encourage code reuse within an organization. Using classes can also reduce errors and enhance reliability by ensuring that all access to the master page occurs in an organized manner.
Direct Object Manipulation
Don't let anyone fool you. If your sole intent in designing a class for a master page is to access objects that master page contains, you do have other options, including direct object manipulation. For instance, consider an example where a master page contains a label with a greeting that must change depending on the user. The reason you include the greeting in the master page, rather than the content page, is to ensure a greeting of some kind appears and because the greeting element naturally fits within the heading area of the form. You could use the code in Listing 1 to access this object directly.
Listing 1: Changing an Object Directly
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
' Begin by locating the form.
For Each FormControl As Control In Master.Controls
If FormControl.ID = "form1" Then
' After locating the form, look for the greeting label.
For Each ChildControl As Control In FormControl.Controls
If ChildControl.ID = "lblGreeting" Then
' After locating the label, modify it.
CType(ChildControl, Label).Text = "Changed Greeting"
End If
Next
End If
Next
End Sub
If you're thinking that this is ugly code, you're right; but it works fine. The code begins by looking for the form in the ControlCollection object, Controls, for the MasterPage object. The MasterPage object contains all of the available information for the master page. (You always use the MasterPage object when working with the Master page.)
Once the code locates the form, it must place this object in a variable and perform the process again. This time, the code looks for the greeting label within the Controls collection of the form. The only problem with this search is that Visual Basic views all of the controls as Control objects, so you can't access the text property. That's why the code includes a call to CType() to convert ChildControl (the object that points to the label) to a Label object. At this point, you can change the Text property to some other value.
Providing Property Access
If the entire process of accessing objects in the master page leaves you a little cold and, perhaps, a little confused, you have an alternative. You can always modify the master page class to include a property for changing the greeting text. You'll need to change the class name of the master page first to something other than _Default. Make sure you change both the ASPX and the code behind files. After you make this change, add a property, as shown here.
Public Property GreetingText() As String
Get
Return lblGreeting.Text
End Get
Set(ByVal value As String)
lblGreeting.Text = value
End Set
End Property
This is a standard property construction that any .NET developer can understand quite easily. In addition, you can add code to ensure the input information is correct and even provide the developer using your master page with information on using the property correctly. Adding a property to change the greeting text shortens the content page code considerably, as shown here.
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
' Change the greeting text.
CType(Master, MyMasterPage).GreetingText = "Changed Greeting"
End Sub
You still have to use the CType() method. It turns out that the .NET Framework always assumes that the Master object is of type MasterPage. Consequently, you still have to convert it to the actual class of your master page. The point is that the access is precise, controlled, and less subject to errors than the direct access method.
![]() |
|


