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. Advertisements

  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, Dec 19, 2005
    #2
    1. Advertisements

  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
     
    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
     
    Roberto Kohler, Dec 19, 2005
    #4
  5. Phillip Williams, Dec 19, 2005
    #5
  6. Phillip,

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

    Roberto
     
    Roberto Kohler, Dec 19, 2005
    #6
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.