Cannot access strongly typed properties in Master page

S

Steve Franks

I am trying to access strongly typed properties in a page's master page from
the content page. I followed an article that describes how to do this, and
it seems quite straight forward. However when I try to access the property
from my content page intellisense does not show the public property.

Likewise when I go to compile I get an error saying that

Specifically here is the article I am trying to follow:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/aspnet-masterpages.asp

It says "... there is a much easier and more object-oriented approach. Since
the Master property is strongly typed any public properties exposed by the
MasterPage class, including those you create, can be early-bound. So if I
create two string properties, Title and Keywords, along with their
associated data members in the code-behind file:

protected String m_HtmlTitle =
"http://www.dotnetdashboard.net";

public String HtmlTitle
{
get { return m_HtmlTitle; }
set { m_HtmlTitle = value; }

I put that above code in the Code Behind page in my Master page. Then I
created a new web form and pointed at that master page as the pages master.
In this new web form's Page_OnLoad I put this code:
this.Master.HtmlTitle = "This is a test";

But when I press the "." after "Master" I do not get any Intellisense option
for "HtmlTitle". Likewise when I compile I get the following error:
Error 2 'System.Web.UI.MasterPage' does not contain a definition for
'HtmlTitle' D:\mytest\test.aspx.cs

If I take that line out the page does load, and its definately pulling in
content from that master page just fine.

I realize there are other ways to override the title. However I would like
to use this technique of adding strongly typed public properties for other
purposes. So I'd like to figure out why it is not working. I think I am
following the example but must be missing something.

Does anyone know what I may be doing wrong or how to get this to work as the
article describes?

** UPDATE: Just before I sent this message it occurred to me that perhaps I
needed some sort of cast. My master page is called MyTemplate and I noticed
that in MyTemplate its defined as a class which derrives from MasterPage.

So I decided to try this, and THIS WORKS!
((MyTemplate)this.Master).HtmlTitle = "THIS IS A TEST";

However - this is quite awkward to write, and the articles I read do not
mention the need to do this. So my questions are:

1) Is using a cast like this really the correct way to do this?

2) Is this a more elegant way or a way that does not require an explict cast
like this? It seems odd that this would be the right way, especially given
that no articles on the subject talk about the need to do this.

Thanks,

Steve
 
S

Steve Franks

In digging around more I may have discovered the answer. Aparrently you
need to add something like:
<%@ MasterType VirtualPath="~/MyTemplate.master" %>

If you want to avoid the cast. Adding that allows me to directly access
this.Master.HtmlTitle.

Does anyone know if there is a way to globally specific that MasterType so
that I do not have to repeat it on every page?

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top