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.

6 Comments

  • You should be able to loop through the page's controls and find the HtmlForm without any problem -- just make sure you are recursively looping through each control's child controls as well!



    Thanks, Paul Wilson

  • Hi,



    I can't seem to get this to work, I believe my problem is I don't have a control named <control id>_Template on my page



    Could you email me if you have a solution to this?



    gabrielhesse @ hotmail.com

  • Turn on tracing to see what you can "find" -- it will be the master, not one of the regions.

  • Does this work with sub proceedures as well? I am trying to call a sub proceedure located in my template from my implimenting page. Any ideas?

  • Where does this code go...on the template page? or on the page using the template? When I add it to the page using the template I get an error saying MyTemplate is not defined...my class is called MyTemplate



    For Instance I have this in my template code behind...



    Public Class MyTemplate

    Inherits System.Web.UI.UserControl



    as my class declaration..along with all the other proper stuff in there...



    Now when I put the code you gave us above





    Private ReadOnly Property Master() As MyTemplate

    Get

    Return CType(FindControl("mstrPage_Template"), MyTemplate)

    End Get

    End Property





    Into my template code behind...either it doesnt work..or I don't know how to reference it...



    When I put it into my page code I get MyTemplate not defined..



    When I use it with your code in my template page...then try to call a sub procedure (located in my template code behind) called ButtonLib() it doesn't work..no matter how I preface: MyTemplate.ButtonLib(), Master.ButtonLib() etc...just don't know how to call it.

  • You put this code in the page itself, or in a base page if you are using that approach. If MyTemplate is not defined then either its not compiled or you have the namespace/class name incorrect. I notice you are using VB, and that automatically prepends the name of your project to all your namespace/class names, unless you change this setting in the project file. I can't think of anything else.

Comments have been disabled for this content.