473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to programmatically add onLoad and onUnload handlers to BODY tag in ASP.NET 2.0

Well the subject says it all but I am going to elaborate a bit.

As we all now since 2.0 it is possible to access the Header from code behind
without much wizardry and that is a good thing.

Now, in my site I am using master pages and one of the site pages (but could
be more) needs certain customization in which the BODY tag needs the onLoad
and onUnload events added to it:

[body onload="load()" onunload="GEUnload()"]

I don't want to add that to the master page because of just a few pages. I
would like to be able to do that programmatically without having to create a
shadow master page that would have these attributes.

I tried putting the id and runat attributes to the BODY tag on the master
page but that did not work out because ASP.NET changes the ID of BODY to
something like ct100_body rather than the one I gave.

Can anybody point me to a good solution?
Mar 19 '07 #1
5 17985
"~~~ .NET Ed ~~~" <ti*********@abolishspam.nowwrote in message
news:e8**************@TK2MSFTNGP05.phx.gbl...
Well the subject says it all but I am going to elaborate a bit.

As we all now since 2.0 it is possible to access the Header from code
behind without much wizardry and that is a good thing.

Now, in my site I am using master pages and one of the site pages (but
could be more) needs certain customization in which the BODY tag needs the
onLoad and onUnload events added to it:

[body onload="load()" onunload="GEUnload()"]

I don't want to add that to the master page because of just a few pages. I
would like to be able to do that programmatically without having to create
a shadow master page that would have these attributes.

I tried putting the id and runat attributes to the BODY tag on the master
page but that did not work out because ASP.NET changes the ID of BODY to
something like ct100_body rather than the one I gave.

Can anybody point me to a good solution?
In that case, use YourBodyID.ClientID instead of YourBodyID.

--

Riki
Mar 19 '07 #2
Remember that my body ID is "Body" and that ASP.NET converts it to something
like "ctl100_Body" which may change. I tried using FindControl("Body") but
returns null so I can't find the server control and since I don't have that
I don't have any object to use the ClientID property on.

"Riki" <ri**@dontnagme.comwrote in message
news:ee**************@TK2MSFTNGP06.phx.gbl...
>
In that case, use YourBodyID.ClientID instead of YourBodyID.

--

Riki

Mar 19 '07 #3
So you're trying to access it from within the page, not from within the
master page?

Try this (I'm using VB.NET):

CType(Page.Master,MyMasterClassName).Body.ClientID

Replace MyMasterClassName with the real class name of your master page.
Don't use quotes.

However, it's a bad idea to try to access the master from within the page.
It's against OOP principles (code in the page should not depend on code in
the master).

Maybe you can achieve what you want with the
RegisterClientStartupScript method?

--

Riki

"~~~ .NET Ed ~~~" <ti*********@abolishspam.nowwrote in message
news:OT**************@TK2MSFTNGP06.phx.gbl...
Remember that my body ID is "Body" and that ASP.NET converts it to
something like "ctl100_Body" which may change. I tried using
FindControl("Body") but returns null so I can't find the server control
and since I don't have that I don't have any object to use the ClientID
property on.

"Riki" <ri**@dontnagme.comwrote in message
news:ee**************@TK2MSFTNGP06.phx.gbl...
>>
In that case, use YourBodyID.ClientID instead of YourBodyID.

--

Riki


Mar 19 '07 #4
That is right, on my page's OnPreRender method I tried to do those things
without success.

I managed to derive the right control name given by ASP.NET using the
following:

private string DeriveClientID(string programmaticId, HtmlHead head)
{
string ctlPrefix = head.ClientID.Replace(head.ID, "");
return string.Format("{0}{1}", ctlPrefix, programmaticId);
}

so if my HEAD server element (accessed in ASP.NET 2.0 with the Header
property of the page) has ID="Head1" and ClientID="ctl00_Head1" then that
means that if I set BODY id="Body" using this method would return me
"ctl00_Body" as the client ID which in theory I could have used to find the
right control.

The problem is.... even when you use FindControl on the page with the
correct ID string (ctl00_Body) that appears rendered on the page, it would
still not find the control.

Control body = FindControl(DeriveClientID("Body", Header));

would still return body == null even when the ID is the correct one. As it
appears the HEADER is ultra special because if you use the id and runat on
the BODY tag it would still not be accessible by the page.

For curiosity I put the BODY tag within the hierarchy of the FORM tag on the
asp.net page but the results where the same, it still cannot find the
control.

Also tried as someone suggested, by registering a client script block that
would set the window.onload and window.onunload to set the functions to be
called but that does not work either.

Any other ideas?
"Riki" <ri**@dontnagme.comwrote in message
news:Ob**************@TK2MSFTNGP05.phx.gbl...
So you're trying to access it from within the page, not from within the
master page?

Try this (I'm using VB.NET):

CType(Page.Master,MyMasterClassName).Body.ClientID

Replace MyMasterClassName with the real class name of your master page.
Don't use quotes.

However, it's a bad idea to try to access the master from within the page.
It's against OOP principles (code in the page should not depend on code in
the master).

Maybe you can achieve what you want with the
RegisterClientStartupScript method?

--

Riki

"~~~ .NET Ed ~~~" <ti*********@abolishspam.nowwrote in message
news:OT**************@TK2MSFTNGP06.phx.gbl...
>Remember that my body ID is "Body" and that ASP.NET converts it to
something like "ctl100_Body" which may change. I tried using
FindControl("Body") but returns null so I can't find the server control
and since I don't have that I don't have any object to use the ClientID
property on.

"Riki" <ri**@dontnagme.comwrote in message
news:ee**************@TK2MSFTNGP06.phx.gbl...
>>>
In that case, use YourBodyID.ClientID instead of YourBodyID.

--

Riki



Mar 19 '07 #5
Hi,

I had the same problem, don't know whether you've solved it now but here's the code that did it for me (ps remember to add <body runat="server" ID="masterBody"to the master page body element.
This code should go in the OnLoad method of your content page.

Dim control As System.Web.UI.HtmlControls.HtmlGenericControl = Page.Master.FindControl("masterBody")

control.Attributes.Item("onload") = "javascript: LoadForm();"

Hope this helps

EggHeadCafe.com - .NET Developer Portal of Choice
http://www.eggheadcafe.com
Mar 27 '07 #6

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

Similar topics

2
by: saayan | last post by:
Hi, I have a php generated HTML page, which has a javascript call in the body tag: <body onunload="exitPage()" onload="setWidth()" onresize="setWidth()"> In certain cases, there can be an...
7
by: Tery Griffin | last post by:
Hi all, Iıve been away from Javascript for awhile and am rusty, but this is too simple not to work! Iım on a Mac, and usually use the Safari browser. I have a very basic web page in a frame. ...
1
by: Dung Ping | last post by:
For instance, one set is: <body onload="blinking_header()" onunload="stoptimer()"> Another set is: <body onload="writemsg()" onunload="stoptimer()"> They represent two functions. How to...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
4
by: UJ | last post by:
I have some javascript code that I need to run based on a value I know at the server when I load a page. How can I do an onload event call with a condition value from the server. (The reason is...
1
by: Laurent Bugnion | last post by:
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...
4
by: Adam Ratcliffe | last post by:
I have 2 onload handlers on a web page. One is set programatically in an included script and the other declared on the document's body element. In practice the programatically set onload handler...
9
by: pamelafluente | last post by:
Hi It's 2 days I am struggling with 1 stupid line of code. I have the following function which works if I do not do the dynamic code insert: document.body.innerHTML += "<form name='form1'...
0
acoder
by: acoder | last post by:
Problem onload and onunload events do not fire when going back, forward or refreshing the page Browser Opera Example Any code using onload or onunload, e.g. window.onload = init; where...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.