473,804 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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=serv er" 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=serv er" 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=serv er" on a body tag
with an onunload event handler?

I am using the framework 1.1.

Unfortunately, there are no "RegisterEndScr ipt" methods (similar to
"RegisterStartu pScript") 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
Jan 21 '06 #1
1 5328
You can use the window object. Example:

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

Page.RegisterCl ientScriptBlock ("unload",sc r);

--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=serv er" 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=serv er" 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=serv er" on a body tag
with an onunload event handler?

I am using the framework 1.1.

Unfortunately, there are no "RegisterEndScr ipt" methods (similar to
"RegisterStartu pScript") 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

Jan 21 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6739
by: Robert Oschler | last post by:
I have a web page where I want to make sure that an image HTTP request is successfully made before the page unloads. The request is made by setting the image element's SRC property to the desired URL. When I put the SRC property change in either an "onbeforeunload" or "onunload" event handler, the HTTP request is not received by our server as much as 75% of the time. I thought of setting a flag using an "onload" handler for the image...
7
2965
by: Michel | last post by:
Hi all, Is there some intelligent way to trigger a clientside JavaScript function before a postback is triggerd by any of the controls on a page? Thanks in advance, Michel Mathijssen
20
12897
by: David | last post by:
I have a one-line script to add an onunload event handler to the body of the document. The script is as follows: document.getElementsByTagName("BODY").onunload=function s() {alert("s")} Now obviously, I put the alert("s") part in for debugging purposes, just to make sure the error wasn't in any code I was going to be running. This line works just fine in IE6 but in Firefox it doesn't. However, if I replace that line with the...
2
1206
by: Oleg Ogurok | last post by:
Hi all, Is there a way to add a client side onunload statement into the <body> tag of a page? Thanks. -Oleg.
0
9593
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10595
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10343
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9169
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7633
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.