Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Cannot access strongly typed properties in Master page

Reply
Thread Tools

Cannot access strongly typed properties in Master page

 
 
Steve Franks
Guest
Posts: n/a
 
      09-19-2005
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/de...asterpages.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


 
Reply With Quote
 
 
 
 
Steve Franks
Guest
Posts: n/a
 
      09-19-2005
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


 
Reply With Quote
 
 
 
 
Scott Allen
Guest
Posts: n/a
 
      09-19-2005

Hi Steve:

Yes, you can use the MasterType property.
http://odetocode.com/Blogs/scott/arc...7/16/1944.aspx

Although you can set the masterPageFile globally in web.config I don't
believe you can set MasterType there, unfortunately.

--
Scott
http://www.OdeToCode.com/blogs/scott/



On Sun, 18 Sep 2005 23:20:20 -0400, "Steve Franks"
<(E-Mail Removed)> wrote:

>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/de...asterpages.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
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Master Pages - Strongly-Typed control access =?Utf-8?B?Qm9yaXMgWWVsdHNpbg==?= ASP .Net 5 11-10-2006 10:21 PM
accessing strongly typed properties of a Datatable when binding through the ObjectDataSource and Container.DataItem John Davis ASP .Net 0 08-31-2006 11:56 AM
accessing strongly typed properties of a datatable when binding through the objectdatasource and Container.DataItem John Davis ASP .Net 0 08-30-2006 08:28 AM
Custom Namespace For Strongly Typed DataSet Narayanan Sankaranarayanan ASP .Net 0 12-15-2004 01:28 AM
Strongly Typed dataset Imran ASP .Net 1 01-23-2004 07:42 AM



Advertisments