469,946 Members | 1,805 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,946 developers. It's quick & easy.

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 17814
"~~~ .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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by saayan | last post: by
7 posts views Thread by Tery Griffin | last post: by
2 posts views Thread by laredotornado | last post: by
4 posts views Thread by Adam Ratcliffe | last post: by
9 posts views Thread by pamelafluente | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.