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

N

~~~ .NET Ed ~~~

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?
 
R

Riki

~~~ .NET Ed ~~~ said:
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.
 
N

~~~ .NET Ed ~~~

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.
 
R

Riki

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 ~~~ said:
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.
 
N

~~~ .NET Ed ~~~

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 said:
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?
 
D

David Wainwright

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top