How to reference the masterpage body element in server code

Discussion in 'ASP .Net Web Controls' started by Roberto Kohler, Dec 19, 2005.

  1. How do I reference the masterpage body element in ASP.NET 2.0 server code ?

    I tried the following, but I get the error "hgcMasterBody is not a member of
    System.Web.UI.MasterPage"

    In the Master Page:
    <body id="hgcMasterBody" runat="server">
    .....
    </body>

    In the ASPX page:
    <%@ MasterType virtualpath="MyMasterPage.master" %>
    .....
    <script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Master.hgcMasterBody.Attributes("bgcolor") = "lightblue"
    End Sub
    </script>
    Roberto Kohler, Dec 19, 2005
    #1
    1. Advertising

  2. I added a @ MasterType directive to the ASPX page:
    <%@ MasterType VirtualPath="~/MasterPage.master" %>

    and now I get the error:
    Compiler Error Message: BC30390: 'ASP.masterpage_master.Protected Dim
    WithEvents hgcMasterBody As System.Web.UI.HtmlControls.HtmlGenericControl'
    is not accessible in this context because it is 'Protected'.

    Any help would be greatly appreciated.

    ---------------------------------------
    "Roberto Kohler" <> wrote in message
    news:...
    > How do I reference the masterpage body element in ASP.NET 2.0 server code
    > ?
    >
    > I tried the following, but I get the error "hgcMasterBody is not a member
    > of System.Web.UI.MasterPage"
    >
    > In the Master Page:
    > <body id="hgcMasterBody" runat="server">
    > ....
    > </body>
    >
    > In the ASPX page:
    > <%@ MasterType virtualpath="MyMasterPage.master" %>
    > ....
    > <script runat="server">
    > Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    > System.EventArgs)
    > Master.hgcMasterBody.Attributes("bgcolor") = "lightblue"
    > End Sub
    > </script>
    >
    >
    Roberto Kohler, Dec 19, 2005
    #2
    1. Advertising

  3. Hi Roberto,

    I have just created a simple master page with the following code in it and
    it works just fine:
    <body onkeydown="catchKeyPress(window.event.keyCode,
    window.event.srcElement);">

    Here is the link to the test page:
    http://www.webswapp.com/CodeSamples/aspnet20/test1.aspx
    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "Roberto Kohler" wrote:

    > I added a @ MasterType directive to the ASPX page:
    > <%@ MasterType VirtualPath="~/MasterPage.master" %>
    >
    > and now I get the error:
    > Compiler Error Message: BC30390: 'ASP.masterpage_master.Protected Dim
    > WithEvents hgcMasterBody As System.Web.UI.HtmlControls.HtmlGenericControl'
    > is not accessible in this context because it is 'Protected'.
    >
    > Any help would be greatly appreciated.
    >
    > ---------------------------------------
    > "Roberto Kohler" <> wrote in message
    > news:...
    > > How do I reference the masterpage body element in ASP.NET 2.0 server code
    > > ?
    > >
    > > I tried the following, but I get the error "hgcMasterBody is not a member
    > > of System.Web.UI.MasterPage"
    > >
    > > In the Master Page:
    > > <body id="hgcMasterBody" runat="server">
    > > ....
    > > </body>
    > >
    > > In the ASPX page:
    > > <%@ MasterType virtualpath="MyMasterPage.master" %>
    > > ....
    > > <script runat="server">
    > > Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    > > System.EventArgs)
    > > Master.hgcMasterBody.Attributes("bgcolor") = "lightblue"
    > > End Sub
    > > </script>
    > >
    > >

    >
    >
    >
    Phillip Williams, Dec 19, 2005
    #3
  4. Hi Phillip,

    Thanks for replying.

    Your solution works fine if all the aspx pages that use that master-page
    want to use the same event handler.

    The problem that I am trying to solve is how to set the body onkeydown event
    handler from an aspx file that uses the master-page. That is why I'm trying
    to figure out how to reference the masterpage body element from another aspx
    file.

    For example, I could choose to set the onkeydown event handler to null from
    some pages, and to catchKeyPress() from other pages. Another example is, if
    for some strange reason I would need to set the body background color to one
    color from some aspx pages and to a different color from other aspx pages.

    Roberto


    "Phillip Williams" <> wrote in message
    news:...
    > Hi Roberto,
    >
    > I have just created a simple master page with the following code in it and
    > it works just fine:
    > <body onkeydown="catchKeyPress(window.event.keyCode,
    > window.event.srcElement);">
    >
    > Here is the link to the test page:
    > http://www.webswapp.com/CodeSamples/aspnet20/test1.aspx
    > --
    > HTH,
    > Phillip Williams
    > http://www.societopia.net
    > http://www.webswapp.com
    >
    >
    > "Roberto Kohler" wrote:
    >
    >> I added a @ MasterType directive to the ASPX page:
    >> <%@ MasterType VirtualPath="~/MasterPage.master" %>
    >>
    >> and now I get the error:
    >> Compiler Error Message: BC30390: 'ASP.masterpage_master.Protected Dim
    >> WithEvents hgcMasterBody As
    >> System.Web.UI.HtmlControls.HtmlGenericControl'
    >> is not accessible in this context because it is 'Protected'.
    >>
    >> Any help would be greatly appreciated.
    >>
    >> ---------------------------------------
    >> "Roberto Kohler" <> wrote in message
    >> news:...
    >> > How do I reference the masterpage body element in ASP.NET 2.0 server
    >> > code
    >> > ?
    >> >
    >> > I tried the following, but I get the error "hgcMasterBody is not a
    >> > member
    >> > of System.Web.UI.MasterPage"
    >> >
    >> > In the Master Page:
    >> > <body id="hgcMasterBody" runat="server">
    >> > ....
    >> > </body>
    >> >
    >> > In the ASPX page:
    >> > <%@ MasterType virtualpath="MyMasterPage.master" %>
    >> > ....
    >> > <script runat="server">
    >> > Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    >> > System.EventArgs)
    >> > Master.hgcMasterBody.Attributes("bgcolor") = "lightblue"
    >> > End Sub
    >> > </script>
    >> >
    >> >

    >>
    >>
    >>
    Roberto Kohler, Dec 19, 2005
    #4
  5. Ok, I got you. I modified the sample with a new link to another sample to
    demonstrate how to access a control on a master page from the content page:
    http://www.webswapp.com/CodeSamples/aspnet20/test2.aspx
    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "Roberto Kohler" wrote:

    > Hi Phillip,
    >
    > Thanks for replying.
    >
    > Your solution works fine if all the aspx pages that use that master-page
    > want to use the same event handler.
    >
    > The problem that I am trying to solve is how to set the body onkeydown event
    > handler from an aspx file that uses the master-page. That is why I'm trying
    > to figure out how to reference the masterpage body element from another aspx
    > file.
    >
    > For example, I could choose to set the onkeydown event handler to null from
    > some pages, and to catchKeyPress() from other pages. Another example is, if
    > for some strange reason I would need to set the body background color to one
    > color from some aspx pages and to a different color from other aspx pages.
    >
    > Roberto
    >
    >
    > "Phillip Williams" <> wrote in message
    > news:...
    > > Hi Roberto,
    > >
    > > I have just created a simple master page with the following code in it and
    > > it works just fine:
    > > <body onkeydown="catchKeyPress(window.event.keyCode,
    > > window.event.srcElement);">
    > >
    > > Here is the link to the test page:
    > > http://www.webswapp.com/CodeSamples/aspnet20/test1.aspx
    > > --
    > > HTH,
    > > Phillip Williams
    > > http://www.societopia.net
    > > http://www.webswapp.com
    > >
    > >
    > > "Roberto Kohler" wrote:
    > >
    > >> I added a @ MasterType directive to the ASPX page:
    > >> <%@ MasterType VirtualPath="~/MasterPage.master" %>
    > >>
    > >> and now I get the error:
    > >> Compiler Error Message: BC30390: 'ASP.masterpage_master.Protected Dim
    > >> WithEvents hgcMasterBody As
    > >> System.Web.UI.HtmlControls.HtmlGenericControl'
    > >> is not accessible in this context because it is 'Protected'.
    > >>
    > >> Any help would be greatly appreciated.
    > >>
    > >> ---------------------------------------
    > >> "Roberto Kohler" <> wrote in message
    > >> news:...
    > >> > How do I reference the masterpage body element in ASP.NET 2.0 server
    > >> > code
    > >> > ?
    > >> >
    > >> > I tried the following, but I get the error "hgcMasterBody is not a
    > >> > member
    > >> > of System.Web.UI.MasterPage"
    > >> >
    > >> > In the Master Page:
    > >> > <body id="hgcMasterBody" runat="server">
    > >> > ....
    > >> > </body>
    > >> >
    > >> > In the ASPX page:
    > >> > <%@ MasterType virtualpath="MyMasterPage.master" %>
    > >> > ....
    > >> > <script runat="server">
    > >> > Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    > >> > System.EventArgs)
    > >> > Master.hgcMasterBody.Attributes("bgcolor") = "lightblue"
    > >> > End Sub
    > >> > </script>
    > >> >
    > >> >
    > >>
    > >>
    > >>

    >
    >
    >
    Phillip Williams, Dec 19, 2005
    #5
  6. Phillip,

    That's it!
    Thank you so very much...

    Roberto
    Roberto Kohler, Dec 19, 2005
    #6
    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. =?Utf-8?B?QWxleCBNYWdoZW4=?=

    MasterPage -> Page -> MasterPage Code Access

    =?Utf-8?B?QWxleCBNYWdoZW4=?=, Apr 10, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    7,870
    Steven Cheng[MSFT]
    Apr 14, 2006
  2. ThunderMusic
    Replies:
    2
    Views:
    434
    Gregory Gadow
    Sep 27, 2006
  3. Randy Smith
    Replies:
    9
    Views:
    411
    Mark Rae
    Apr 14, 2007
  4. mr t
    Replies:
    0
    Views:
    390
  5. Torben Laursen
    Replies:
    4
    Views:
    568
    clintonG
    Mar 15, 2008
Loading...

Share This Page