How can I access a Master Page Public Property from a Base Page

Discussion in 'ASP .Net' started by =?Utf-8?B?SmF5IFBvbmR5?=, Nov 9, 2007.

  1. I am trying to access a Public property on a Master Page from a Base Page.

    On the content pages I have the MasterType Directive set up as follows:
    <%@ MasterType virtualpath="~/Master.master" %>

    On the Master Page I have a public property exposed:
    Public Property ErrorMessage() As String
    Get
    Return txtError.InnerText
    End Get
    Set(ByVal value As String)
    txtError.InnerText = value
    rowError.Visible = True
    End Set
    End Property


    From the Content Page I can access the property:
    Me.Master.ErrorMessage = "Some Error Message."

    If I try and access the Master Page Property from an inherited Base Page with:
    Me.Master.ErrorMessage = "Some Error Message."
    the IDE highlights the error with:
    ErrorMessage is not a member of System.Web.UI.MasterPage

    Is there some way I can cast the Base Page Master property to the strongly
    typed Master Page and access the ErrorMessage property?
     
    =?Utf-8?B?SmF5IFBvbmR5?=, Nov 9, 2007
    #1
    1. Advertising

  2. On Nov 9, 12:03 pm, Jay Pondy <> wrote:
    > I am trying to access a Public property on a Master Page from a Base Page.
    >
    > On the content pages I have the MasterType Directive set up as follows:
    > <%@ MasterType virtualpath="~/Master.master" %>
    >
    > On the Master Page I have a public property exposed:
    > Public Property ErrorMessage() As String
    > Get
    > Return txtError.InnerText
    > End Get
    > Set(ByVal value As String)
    > txtError.InnerText = value
    > rowError.Visible = True
    > End Set
    > End Property
    >
    > From the Content Page I can access the property:
    > Me.Master.ErrorMessage = "Some Error Message."
    >
    > If I try and access the Master Page Property from an inherited Base Page with:
    > Me.Master.ErrorMessage = "Some Error Message."
    > the IDE highlights the error with:
    > ErrorMessage is not a member of System.Web.UI.MasterPage
    >
    > Is there some way I can cast the Base Page Master property to the strongly
    > typed Master Page and access the ErrorMessage property?


    Dim myMaster As New {masterClassName}
    myMaster.ErrorMessage = "Some Error Message."
     
    Alexey Smirnov, Nov 9, 2007
    #2
    1. Advertising

  3. =?Utf-8?B?SmF5IFBvbmR5?=

    Jay Pondy Guest

    Almost there but...

    I created a BaseMaster class that inherits from System.Web.UI.MasterPage And
    placed my Public Property there:


    Public Class BaseMaster : Inherits System.Web.UI.MasterPage

    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Handles Me.Load
    'Make the error table row invisible
    CType(Page.Master.FindControl("rowError"), HtmlTableRow).Visible = False
    End Sub


    Public Property ErrorMessage() As String
    Get
    Return CType(Page.Master.FindControl("txtError"),
    HtmlGenericControl).InnerText
    End Get
    Set(ByVal value As String)
    Trace.Write("BaseMaster.ErrorMessage.Set")
    CType(Page.Master.FindControl("txtError"),
    HtmlGenericControl).InnerText = value
    CType(Page.Master.FindControl("rowError"), HtmlTableRow).Visible =
    True
    End Set
    End Property


    End Class


    In my Master Page I now inherit from BaseMaster and I CAN now access the
    Property from my content pages:
    CType(Page.Master, BaseMaster).ErrorMessage = "Some Error Message."

    BUT...

    All of my Pages inherit from a BasePage class that contains a Page_Error handler
    from where I would like to access the MasterPage property when an error occurs:

    Public Class BasePage : Inherits System.Web.UI.Page

    Private Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs)
    Handles MyBase.Error
    CType(Page.Master, BaseMaster).ErrorMessage =
    Server.GetLastError.Message
    End Sub

    End Class


    The problem now is that when I generate an error on the content page the Page
    Error handler in my BasePage class fires and it sets the public property in my
    BaseMaster but nothing is displayed on the page (the URL is correct for the
    content page) the only HTML rendered is:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML><HEAD>
    <META http-equiv=Content-Type content="text/html; charset=windows-1252"></HEAD>
    <BODY></BODY></HTML>

    There is no Master and no Content HTML. I'm missing a call to something
    somewhere.

    On Fri, 9 Nov 2007 03:03:01 -0800, Jay Pondy <>
    wrote:

    >I am trying to access a Public property on a Master Page from a Base Page.
    >
    >On the content pages I have the MasterType Directive set up as follows:
    > <%@ MasterType virtualpath="~/Master.master" %>
    >
    >On the Master Page I have a public property exposed:
    > Public Property ErrorMessage() As String
    > Get
    > Return txtError.InnerText
    > End Get
    > Set(ByVal value As String)
    > txtError.InnerText = value
    > rowError.Visible = True
    > End Set
    > End Property
    >
    >
    >From the Content Page I can access the property:
    > Me.Master.ErrorMessage = "Some Error Message."
    >
    >If I try and access the Master Page Property from an inherited Base Page with:
    > Me.Master.ErrorMessage = "Some Error Message."
    >the IDE highlights the error with:
    > ErrorMessage is not a member of System.Web.UI.MasterPage
    >
    >Is there some way I can cast the Base Page Master property to the strongly
    >typed Master Page and access the ErrorMessage property?
     
    Jay Pondy, Nov 9, 2007
    #3
  4. =?Utf-8?B?SmF5IFBvbmR5?=

    Patrice Guest

    Unclear. The last part would make me think you try to access in a base class
    to a property you defined in a derived class. This is the other way round
    (i.e. what a base class is allowed to do is automatically doable by its
    derived classes).

    --
    Patrice

    "Alexey Smirnov" <> a écrit dans le message de news:
    ...
    > On Nov 9, 12:03 pm, Jay Pondy <> wrote:
    >> I am trying to access a Public property on a Master Page from a Base
    >> Page.
    >>
    >> On the content pages I have the MasterType Directive set up as follows:
    >> <%@ MasterType virtualpath="~/Master.master" %>
    >>
    >> On the Master Page I have a public property exposed:
    >> Public Property ErrorMessage() As String
    >> Get
    >> Return txtError.InnerText
    >> End Get
    >> Set(ByVal value As String)
    >> txtError.InnerText = value
    >> rowError.Visible = True
    >> End Set
    >> End Property
    >>
    >> From the Content Page I can access the property:
    >> Me.Master.ErrorMessage = "Some Error Message."
    >>
    >> If I try and access the Master Page Property from an inherited Base Page
    >> with:
    >> Me.Master.ErrorMessage = "Some Error Message."
    >> the IDE highlights the error with:
    >> ErrorMessage is not a member of System.Web.UI.MasterPage
    >>
    >> Is there some way I can cast the Base Page Master property to the
    >> strongly
    >> typed Master Page and access the ErrorMessage property?

    >
    > Dim myMaster As New {masterClassName}
    > myMaster.ErrorMessage = "Some Error Message."
    >
     
    Patrice, Nov 9, 2007
    #4
  5. =?Utf-8?B?SmF5IFBvbmR5?=

    bruce barker Guest

    even better than a cast, tell asp.net to do it.

    <%@ MasterType TypeName="MyMaster" %>

    -- bruce (sqlwork.com)


    Jay Pondy wrote:
    > I am trying to access a Public property on a Master Page from a Base Page.
    >
    > On the content pages I have the MasterType Directive set up as follows:
    > <%@ MasterType virtualpath="~/Master.master" %>
    >
    > On the Master Page I have a public property exposed:
    > Public Property ErrorMessage() As String
    > Get
    > Return txtError.InnerText
    > End Get
    > Set(ByVal value As String)
    > txtError.InnerText = value
    > rowError.Visible = True
    > End Set
    > End Property
    >
    >
    > From the Content Page I can access the property:
    > Me.Master.ErrorMessage = "Some Error Message."
    >
    > If I try and access the Master Page Property from an inherited Base Page with:
    > Me.Master.ErrorMessage = "Some Error Message."
    > the IDE highlights the error with:
    > ErrorMessage is not a member of System.Web.UI.MasterPage
    >
    > Is there some way I can cast the Base Page Master property to the strongly
    > typed Master Page and access the ErrorMessage property?
    >
     
    bruce barker, Nov 9, 2007
    #5
  6. Hi Jay,

    As for accessing propertes in Master page, you can also consider the
    following means:

    #define a public interface which contains the properties or method you need
    #let master implement the interface and in content page, simply cast Master
    page to that interface and call those interface methods. e.g.

    =========Interface===================
    public interface IError
    {
    string ErrorInfo {get;set; }
    }

    =========Master page==============
    public partial class Masters_err : System.Web.UI.MasterPage, IError
    {
    ......................
    ======================

    thus, you do not need to define base classes. For the problem that the
    page doesn't display any UI after "Page_Error" event, I think this is
    because when there occurs unhandled exception, the original page processing
    sequence is broken, therefore, you can not expect it to render the page
    content as normal (if there occurs unhandled exception). The "Page_Error"
    or "Application_Error" event just provide you a point to capture this error
    status and the recommended approach to show error info is defining a custom
    error page or manually do redirection in "Page_Error" event (to your own
    error page). You can store the last error info in SessionState if necessary.


    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    This posting is provided "AS IS" with no warranties, and confers no rights.







    --------------------
    >From: Jay Pondy <>
    >Newsgroups: microsoft.public.dotnet.framework.aspnet
    >Subject: Re: How can I access a Master Page Public Property from a Base

    Page
    >Date: Fri, 09 Nov 2007 07:18:50 -0500


    >
    >Almost there but...
    >
    >I created a BaseMaster class that inherits from System.Web.UI.MasterPage

    And
    >placed my Public Property there:
    >
    >
    >Public Class BaseMaster : Inherits System.Web.UI.MasterPage
    >
    > Private Sub Page_Load(ByVal sender As Object, ByVal e As

    System.EventArgs)
    >Handles Me.Load
    > 'Make the error table row invisible
    > CType(Page.Master.FindControl("rowError"), HtmlTableRow).Visible =

    False
    > End Sub
    >
    >
    > Public Property ErrorMessage() As String
    > Get
    > Return CType(Page.Master.FindControl("txtError"),
    >HtmlGenericControl).InnerText
    > End Get
    > Set(ByVal value As String)
    > Trace.Write("BaseMaster.ErrorMessage.Set")
    > CType(Page.Master.FindControl("txtError"),
    >HtmlGenericControl).InnerText = value
    > CType(Page.Master.FindControl("rowError"),

    HtmlTableRow).Visible =
    >True
    > End Set
    > End Property
    >
    >
    >End Class
    >
    >
    >In my Master Page I now inherit from BaseMaster and I CAN now access the
    >Property from my content pages:
    > CType(Page.Master, BaseMaster).ErrorMessage = "Some Error Message."
    >
    >BUT...
    >
    >All of my Pages inherit from a BasePage class that contains a Page_Error

    handler
    >from where I would like to access the MasterPage property when an error

    occurs:
    >
    > Public Class BasePage : Inherits System.Web.UI.Page
    >
    > Private Sub Page_Error(ByVal sender As Object, ByVal e As

    System.EventArgs)
    >Handles MyBase.Error
    > CType(Page.Master, BaseMaster).ErrorMessage =
    >Server.GetLastError.Message
    > End Sub
    >
    > End Class
    >
    >
    >The problem now is that when I generate an error on the content page the

    Page
    >Error handler in my BasePage class fires and it sets the public property

    in my
    >BaseMaster but nothing is displayed on the page (the URL is correct for the
    >content page) the only HTML rendered is:
    >
    ><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    ><HTML><HEAD>
    ><META http-equiv=Content-Type content="text/html;

    charset=windows-1252"></HEAD>
    ><BODY></BODY></HTML>
    >
    >There is no Master and no Content HTML. I'm missing a call to something
    >somewhere.
    >
    >On Fri, 9 Nov 2007 03:03:01 -0800, Jay Pondy <>
    >wrote:
    >
    >>I am trying to access a Public property on a Master Page from a Base Page.
    >>
    >>On the content pages I have the MasterType Directive set up as follows:
    >> <%@ MasterType virtualpath="~/Master.master" %>
    >>
    >>On the Master Page I have a public property exposed:
    >> Public Property ErrorMessage() As String
    >> Get
    >> Return txtError.InnerText
    >> End Get
    >> Set(ByVal value As String)
    >> txtError.InnerText = value
    >> rowError.Visible = True
    >> End Set
    >> End Property
    >>
    >>
    >>From the Content Page I can access the property:
    >> Me.Master.ErrorMessage = "Some Error Message."
    >>
    >>If I try and access the Master Page Property from an inherited Base Page

    with:
    >> Me.Master.ErrorMessage = "Some Error Message."
    >>the IDE highlights the error with:
    >> ErrorMessage is not a member of System.Web.UI.MasterPage
    >>
    >>Is there some way I can cast the Base Page Master property to the

    strongly
    >>typed Master Page and access the ErrorMessage property?

    >
     
    Steven Cheng[MSFT], Nov 12, 2007
    #6
  7. Re: How can I access a Master Page Public Property from a Base Pag

    Thanks Steven.


    "Steven Cheng[MSFT]" wrote:

    > Hi Jay,
    >
    > As for accessing propertes in Master page, you can also consider the
    > following means:
    >
    > #define a public interface which contains the properties or method you need
    > #let master implement the interface and in content page, simply cast Master
    > page to that interface and call those interface methods. e.g.
    >
    > =========Interface===================
    > public interface IError
    > {
    > string ErrorInfo {get;set; }
    > }
    >
    > =========Master page==============
    > public partial class Masters_err : System.Web.UI.MasterPage, IError
    > {
    > ......................
    > ======================
    >
    > thus, you do not need to define base classes. For the problem that the
    > page doesn't display any UI after "Page_Error" event, I think this is
    > because when there occurs unhandled exception, the original page processing
    > sequence is broken, therefore, you can not expect it to render the page
    > content as normal (if there occurs unhandled exception). The "Page_Error"
    > or "Application_Error" event just provide you a point to capture this error
    > status and the recommended approach to show error info is defining a custom
    > error page or manually do redirection in "Page_Error" event (to your own
    > error page). You can store the last error info in SessionState if necessary.
    >
    >
    > Sincerely,
    >
    > Steven Cheng
    >
    > Microsoft MSDN Online Support Lead
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    >
    >
    >
    >
    >
    >
    > --------------------
    > >From: Jay Pondy <>
    > >Newsgroups: microsoft.public.dotnet.framework.aspnet
    > >Subject: Re: How can I access a Master Page Public Property from a Base

    > Page
    > >Date: Fri, 09 Nov 2007 07:18:50 -0500

    >
    > >
    > >Almost there but...
    > >
    > >I created a BaseMaster class that inherits from System.Web.UI.MasterPage

    > And
    > >placed my Public Property there:
    > >
    > >
    > >Public Class BaseMaster : Inherits System.Web.UI.MasterPage
    > >
    > > Private Sub Page_Load(ByVal sender As Object, ByVal e As

    > System.EventArgs)
    > >Handles Me.Load
    > > 'Make the error table row invisible
    > > CType(Page.Master.FindControl("rowError"), HtmlTableRow).Visible =

    > False
    > > End Sub
    > >
    > >
    > > Public Property ErrorMessage() As String
    > > Get
    > > Return CType(Page.Master.FindControl("txtError"),
    > >HtmlGenericControl).InnerText
    > > End Get
    > > Set(ByVal value As String)
    > > Trace.Write("BaseMaster.ErrorMessage.Set")
    > > CType(Page.Master.FindControl("txtError"),
    > >HtmlGenericControl).InnerText = value
    > > CType(Page.Master.FindControl("rowError"),

    > HtmlTableRow).Visible =
    > >True
    > > End Set
    > > End Property
    > >
    > >
    > >End Class
    > >
    > >
    > >In my Master Page I now inherit from BaseMaster and I CAN now access the
    > >Property from my content pages:
    > > CType(Page.Master, BaseMaster).ErrorMessage = "Some Error Message."
    > >
    > >BUT...
    > >
    > >All of my Pages inherit from a BasePage class that contains a Page_Error

    > handler
    > >from where I would like to access the MasterPage property when an error

    > occurs:
    > >
    > > Public Class BasePage : Inherits System.Web.UI.Page
    > >
    > > Private Sub Page_Error(ByVal sender As Object, ByVal e As

    > System.EventArgs)
    > >Handles MyBase.Error
    > > CType(Page.Master, BaseMaster).ErrorMessage =
    > >Server.GetLastError.Message
    > > End Sub
    > >
    > > End Class
    > >
    > >
    > >The problem now is that when I generate an error on the content page the

    > Page
    > >Error handler in my BasePage class fires and it sets the public property

    > in my
    > >BaseMaster but nothing is displayed on the page (the URL is correct for the
    > >content page) the only HTML rendered is:
    > >
    > ><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    > ><HTML><HEAD>
    > ><META http-equiv=Content-Type content="text/html;

    > charset=windows-1252"></HEAD>
    > ><BODY></BODY></HTML>
    > >
    > >There is no Master and no Content HTML. I'm missing a call to something
    > >somewhere.
    > >
    > >On Fri, 9 Nov 2007 03:03:01 -0800, Jay Pondy <>
    > >wrote:
    > >
    > >>I am trying to access a Public property on a Master Page from a Base Page.
    > >>
    > >>On the content pages I have the MasterType Directive set up as follows:
    > >> <%@ MasterType virtualpath="~/Master.master" %>
    > >>
    > >>On the Master Page I have a public property exposed:
    > >> Public Property ErrorMessage() As String
    > >> Get
    > >> Return txtError.InnerText
    > >> End Get
    > >> Set(ByVal value As String)
    > >> txtError.InnerText = value
    > >> rowError.Visible = True
    > >> End Set
    > >> End Property
    > >>
    > >>
    > >>From the Content Page I can access the property:
    > >> Me.Master.ErrorMessage = "Some Error Message."
    > >>
    > >>If I try and access the Master Page Property from an inherited Base Page

    > with:
    > >> Me.Master.ErrorMessage = "Some Error Message."
    > >>the IDE highlights the error with:
    > >> ErrorMessage is not a member of System.Web.UI.MasterPage
    > >>
    > >>Is there some way I can cast the Base Page Master property to the

    > strongly
    > >>typed Master Page and access the ErrorMessage property?

    > >

    >
    >
     
    =?Utf-8?B?SmF5IFBvbmR5?=, Nov 14, 2007
    #7
    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. Charles A. Lackman
    Replies:
    1
    Views:
    1,445
    smith
    Dec 8, 2004
  2. SpamProof
    Replies:
    0
    Views:
    641
    SpamProof
    Oct 21, 2003
  3. Kevin Spencer
    Replies:
    2
    Views:
    3,338
    Kevin Spencer
    Sep 15, 2004
  4. Ex glider pilot
    Replies:
    1
    Views:
    412
    clintonG
    May 25, 2008
  5. Nathan Sokalski
    Replies:
    25
    Views:
    1,498
    Nathan Sokalski
    Mar 4, 2010
Loading...

Share This Page