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.

Published Monday, January 12, 2004 8:17 AM by PaulWilson

Comments

# re: MasterPages Template Properties in .NET v1.*

Paul,
I compared Master Pages control ASP.NET 1.x to ASP.NET 2.0 check it out:

http://ipattern.com/simpleblog/PermLink.aspx?entryid=22

Let me know what you think, Maxim

Monday, January 12, 2004 8:45 AM by Maxim V. Karpov

# re: MasterPages Template Properties in .NET v1.*

Let's say at the top-most MasterPage I want to expose the EncType of the Form so my image upload UserControl can set it and set it to accept user uploaded files. For the life of me I couldn't get FindControl to work (is string matching based on the ID the best way? what if the ID changes? if every page has to have a form, shouldn't we be able to find it by the type rather than by its string id?) My current hack in the UserControl is:

((HtmlForm)this.Parent.Parent.Parent).Enctype = "multipart/form-data"; // HACK

That _roughly_ translates to UserControl.AspxPage.Region.ContentContainer.

Depending on whether or not the UserControl is used inside of MasterPages or on a normal Page, that syntax will break.

I've tried adding a property to the top-level MasterPage so I can restrict my UserControl to the Form's EncType only:

public string FormEncType
{
get { return ((HtmlForm)Nobugform1).Enctype; }
set { ((HtmlForm)Nobugform1).Enctype = value; }
}

But I can't get a refernce to the top most page. I'm all about using the following syntax to pass just the HtmlForm into my UserControl so it can work in or out of MasterPages:

uc.TopLevelForm = Page.Form;

or

uc.TopLevelForm = Page.Master.Form;

but those don't work for various accessbility reasons.

Yes, I've tried looping through the Controls collection and looking for a Control of type HtmlForm. As of this writing, my two posts to the Asp.Net forums are still unanswered:

http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=439907

Ideas? I hope its just something simple I'm overlooking.

- Ron

Monday, January 12, 2004 1:31 PM by Ron

# re: MasterPages Template Properties in .NET v1.*

Andy Smith at metabuilders.com has some code that exposes the top most HtmlForm Control:

http://www.metabuilders.com/Tools/FileUpload.aspx

My this.Parent.Parent hack seems to be crude but correct. His EnsureEncType function offers a more robust way of locating the HtmlForm Control.

- Ron

Monday, January 12, 2004 11:54 PM by Ron

# re: MasterPages Template Properties in .NET v1.*

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

Wednesday, January 14, 2004 8:05 AM by Paul Wilson

# re: MasterPages Template Properties in .NET v1.*

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

Wednesday, May 12, 2004 10:09 PM by Gabe

# re: MasterPages Template Properties in .NET v1.*

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

Thursday, May 13, 2004 5:40 AM by Paul Wilson

# re: MasterPages Template Properties in .NET v1.*

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?

Thursday, August 05, 2004 9:28 AM by Daniel Hirsch

# re: MasterPages Template Properties in .NET v1.*

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.

Thursday, August 05, 2004 10:04 AM by Daniel Hirsch

# re: MasterPages Template Properties in .NET v1.*

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.

Thursday, August 05, 2004 9:38 PM by Paul Wilson

Leave a Comment

(required) 
(required) 
(optional)
(required)