Home All Groups Group Topic Archive Search About
Author
19 Dec 2005 1:39 PM
Roberto Kohler
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 ?

Author
19 Dec 2005 2:37 PM
Christopher Reed
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."

Show quoteHide quote
"Roberto Kohler" <rkoh***@intranetslab.com> wrote in message
news:%23NBQEHKBGHA.1312@TK2MSFTNGP09.phx.gbl...
>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 ?
>
>
Author
19 Dec 2005 5:34 PM
Roberto Kohler
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
Author
19 Dec 2005 6:58 PM
Roberto Kohler
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);";
Author
21 Dec 2005 1:57 PM
Christopher Reed
Show quote Hide quote
"Roberto Kohler" <rkoh***@intranetslab.com> wrote in message
news:exryl5MBGHA.1252@TK2MSFTNGP09.phx.gbl...
>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);";
>
>
Author
21 Dec 2005 2:02 PM
Christopher Reed
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."

Show quoteHide quote
"Roberto Kohler" <rkoh***@intranetslab.com> wrote in message
news:exryl5MBGHA.1252@TK2MSFTNGP09.phx.gbl...
>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);";
>
>
Author
21 Dec 2005 10:36 PM
Roberto Kohler
Christopher,
That worked!
Thanks.