MasterPages Template Properties in .NET v1.*
I've been asked one question about my MasterPages for .NET v1.* often enough that I suppose I should blog about it for everyone's benefit. The question is how do you access public properties that have been exposed by the template user control from your pages that implement MasterPages. This will be very easy in .NET v2.0, since MasterPages will contain a Master property in the Page class that will automatically be strongly typed to your master. But how do you do it today, in .NET v1.1?
C# Version:
private MyTemplate Master {
get { return (MyTemplate) FindControl("MyMaster_Template"); }
}
VB.NET Version:
Private ReadOnly Property Master() As MyTemplate
Get
Return CType(FindControl("MyMaster_Template"), MyTemplate)
End Get
End Property
Assumptions:
The user control that contains the template for MasterPages is of type MyTemplate.
The MasterPage control itself is assigned the id "MyMaster" in the page using it.
Extensions:
The Master property can be added to a base page class if the "MyMaster" id is consistent.
The Master property can work with several templates if there is a common base user control.