masterpages: body event handler

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

  1. I am trying to add a body event handler to a page that uses a masterpage.
    I need to do something like:

    <body onkeydown="catchKeyPress(window.event.keyCode,
    window.event.srcElement);">

    The problem is that when the page uses a masterpage, I can't add a <body>
    tag.
    How do I set a body event handler when a page uses masterpages ?
     
    Roberto Kohler, Dec 19, 2005
    #1
    1. Advertising

  2. You would add the body with the onkeydown attribute to the master page.

    Now, if you only want this on some pages, you can try this:

    In the master page:

    <body id="hgcMasterBody" runat="server">
    .....
    </body>

    In the ASPX page:
    <%@ MasterType virtualpath="MyMasterPage.master" %>
    .....
    <script runat="server">
    Master.hgcMasterBody.Attributes["onkeydown"] =
    "catchKeyPress(window.event.keyCode, window.event.srcElement);";
    ......
    </script>
    .....

    I haven't tried this myself, so you may have to tweak it a bit.
    --
    Christopher A. Reed
    "The oxen are slow, but the earth is patient."

    "Roberto Kohler" <> wrote in message
    news:%...
    >I am trying to add a body event handler to a page that uses a masterpage.
    > I need to do something like:
    >
    > <body onkeydown="catchKeyPress(window.event.keyCode,
    > window.event.srcElement);">
    >
    > The problem is that when the page uses a masterpage, I can't add a <body>
    > tag.
    > How do I set a body event handler when a page uses masterpages ?
    >
    >
     
    Christopher Reed, Dec 19, 2005
    #2
    1. Advertising

  3. Christopher,

    Thanks for your response.

    I can't figure out how to reference the masterpage body element in ASP.NET
    2.0 server code.

    As per your suggestion, I tried

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

    I get the error hgcMasterBody is not a member of System.Web.UI.MasterPage
     
    Roberto Kohler, Dec 19, 2005
    #3
  4. 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'.

    This happens in both of these cases:
    Master.hgcMasterBody.Attributes("bgcolor") = "lightblue"
    and
    Master.hgcMasterBody.Attributes["onkeydown"] =
    "catchKeyPress(window.event.keyCode, window.event.srcElement);";
     
    Roberto Kohler, Dec 19, 2005
    #4
  5. "Roberto Kohler" <> wrote in message
    news:...
    >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'.
    >
    > This happens in both of these cases:
    > Master.hgcMasterBody.Attributes("bgcolor") = "lightblue"
    > and
    > Master.hgcMasterBody.Attributes["onkeydown"] =
    > "catchKeyPress(window.event.keyCode, window.event.srcElement);";
    >
    >
     
    Christopher Reed, Dec 21, 2005
    #5
  6. Sorry, I made two mistakes:

    First off, I used the "[" because I was thinking in C#. You need to use "("
    in VB.

    Now, make this change to your Page_Load:

    Dim hgcMasterBody As HtmlGenericControl =
    Master.FindControl("hgcMasterBody")

    hgcMasterBody.Attributes("bgcolor") = "lightblue"
    hgcMasterBody.Attributes("onkeydown") =
    ""catchKeyPress(window.event.keyCode, window.event.srcElement);"

    Ignore my previous ending semicolon as well (another C# thing).

    Hope this helps!
    --
    Christopher A. Reed
    "The oxen are slow, but the earth is patient."

    "Roberto Kohler" <> wrote in message
    news:...
    >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'.
    >
    > This happens in both of these cases:
    > Master.hgcMasterBody.Attributes("bgcolor") = "lightblue"
    > and
    > Master.hgcMasterBody.Attributes["onkeydown"] =
    > "catchKeyPress(window.event.keyCode, window.event.srcElement);";
    >
    >
     
    Christopher Reed, Dec 21, 2005
    #6
  7. Christopher,
    That worked!
    Thanks.
     
    Roberto Kohler, Dec 21, 2005
    #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. Laurent Bugnion
    Replies:
    1
    Views:
    10,060
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Jan 21, 2006
  2. Nick Wouters
    Replies:
    5
    Views:
    4,650
    Flinky Wisty Pomm
    May 8, 2006
  3. Replies:
    1
    Views:
    717
    Damien
    Feb 22, 2007
  4. JohnE

    masterpages with masterpages

    JohnE, Dec 7, 2009, in forum: ASP .Net
    Replies:
    1
    Views:
    498
    Hillbilly
    Dec 8, 2009
  5. szimek
    Replies:
    12
    Views:
    251
    Thomas 'PointedEars' Lahn
    Feb 17, 2008
Loading...

Share This Page