Cannot access strongly typed properties in Master page

Discussion in 'ASP .Net' started by Steve Franks, Sep 19, 2005.

  1. Steve Franks

    Steve Franks Guest

    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
     
    Steve Franks, Sep 19, 2005
    #1
    1. Advertising

  2. Steve Franks

    Steve Franks Guest

    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
     
    Steve Franks, Sep 19, 2005
    #2
    1. Advertising

  3. Steve Franks

    Scott Allen Guest

    Hi Steve:

    Yes, you can use the MasterType property.
    http://odetocode.com/Blogs/scott/archive/2005/07/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"
    <> 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/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
    >
     
    Scott Allen, Sep 19, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Narayanan Sankaranarayanan

    Custom Namespace For Strongly Typed DataSet

    Narayanan Sankaranarayanan, Dec 15, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    1,091
    Narayanan Sankaranarayanan
    Dec 15, 2004
  2. Imran

    Strongly Typed dataset

    Imran, Jan 23, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    568
    Richard A. Lowe
    Jan 23, 2004
  3. =?Utf-8?B?Qm9yaXMgWWVsdHNpbg==?=

    Master Pages - Strongly-Typed control access

    =?Utf-8?B?Qm9yaXMgWWVsdHNpbg==?=, Aug 23, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    3,146
    sjakal
    Nov 10, 2006
  4. John Davis
    Replies:
    0
    Views:
    406
    John Davis
    Aug 30, 2006
  5. John Davis
    Replies:
    0
    Views:
    537
    John Davis
    Aug 31, 2006
Loading...

Share This Page