masterpages: body event handler

R

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 ?
 
C

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

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
 
R

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);";
 
C

Christopher Reed

Roberto Kohler said:
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);";
 
C

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!
 

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

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top