Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > masterpages: body event handler

Reply
Thread Tools

masterpages: body event handler

 
 
Roberto Kohler
Guest
Posts: n/a
 
      12-19-2005
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 ?


 
Reply With Quote
 
 
 
 
Christopher Reed
Guest
Posts: n/a
 
      12-19-2005
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 ?
>
>



 
Reply With Quote
 
 
 
 
Roberto Kohler
Guest
Posts: n/a
 
      12-19-2005
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


 
Reply With Quote
 
Roberto Kohler
Guest
Posts: n/a
 
      12-19-2005
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);";


 
Reply With Quote
 
Christopher Reed
Guest
Posts: n/a
 
      12-21-2005

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



 
Reply With Quote
 
Christopher Reed
Guest
Posts: n/a
 
      12-21-2005
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);";
>
>



 
Reply With Quote
 
Roberto Kohler
Guest
Posts: n/a
 
      12-21-2005
Christopher,
That worked!
Thanks.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
event handler in <body> doesn't work Chris Javascript 2 11-02-2009 04:52 PM
Problem with "this" keyword in event handler of body element szimek Javascript 12 02-17-2008 12:32 PM
Event Handler that creates adds another event handler kaczmar2@gmail.com ASP .Net 1 02-22-2007 07:37 AM
Adding "onunload" JavaScript event handler to body in code-behind Laurent Bugnion ASP .Net 1 01-21-2006 09:53 PM
rendering Button inside Render() event, makes it loose its click event handler association sonic ASP .Net 1 01-07-2005 06:33 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57