Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Adding "onunload" JavaScript event handler to body in code-behind

Reply
Thread Tools

Adding "onunload" JavaScript event handler to body in code-behind

 
 
Laurent Bugnion
Guest
Posts: n/a
 
      01-21-2006
Hi,

I am trying to programatically add an "onunload" event handler to the
"body" tag of an ASPX page. I am doing this from a Custom Control
located on this page.

To do this, I added "runat=server" to the body tag, which provides me
access to it in the code-behind through a HtmlControl. I can then
manipulate the Attributes collection, which is what I needed. So far so
good.

However, I found out that "runat=server" generates a compilation error
(CS1012: Too many characters in character literal) if the "body" tag
already has an onunload event handler (in the ASPX code). This is a
problem, because I wish that the user of my custom control be free to
specify event handlers in the body tag, and my control should simply add
its own.

So the question is: Is there a way to use "runat=server" on a body tag
with an onunload event handler?

I am using the framework 1.1.

Unfortunately, there are no "RegisterEndScript" methods (similar to
"RegisterStartupScript") in the Page class

Thanks,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
 
Reply With Quote
 
 
 
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      01-21-2006
You can use the window object. Example:

string scrp = @"<script>
window.onunload =test;
function test()
{
alert('yo');
}
</script>

Page.RegisterClientScriptBlock("unload",scr);

--Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




"Laurent Bugnion" wrote:

> Hi,
>
> I am trying to programatically add an "onunload" event handler to the
> "body" tag of an ASPX page. I am doing this from a Custom Control
> located on this page.
>
> To do this, I added "runat=server" to the body tag, which provides me
> access to it in the code-behind through a HtmlControl. I can then
> manipulate the Attributes collection, which is what I needed. So far so
> good.
>
> However, I found out that "runat=server" generates a compilation error
> (CS1012: Too many characters in character literal) if the "body" tag
> already has an onunload event handler (in the ASPX code). This is a
> problem, because I wish that the user of my custom control be free to
> specify event handlers in the body tag, and my control should simply add
> its own.
>
> So the question is: Is there a way to use "runat=server" on a body tag
> with an onunload event handler?
>
> I am using the framework 1.1.
>
> Unfortunately, there are no "RegisterEndScript" methods (similar to
> "RegisterStartupScript") in the Page class
>
> Thanks,
> Laurent
> --
> Laurent Bugnion, GalaSoft
> Software engineering: http://www.galasoft-LB.ch
> Private/Malaysia: http://mypage.bluewin.ch/lbugnion
> Support children in Calcutta: http://www.calcutta-espoir.ch
>

 
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
Avoiding &amp; when adding a JavaScript event handler using Attributes.Add() Nathan Sokalski ASP .Net 8 03-01-2006 05:08 AM
masterpages: body event handler Roberto Kohler ASP .Net Web Controls 6 12-21-2005 10:36 PM



Advertisments