how can server control change BODY onload attribute?

K

kw

In a server control, is there any way to access the PAGE and modify the BODY
element ONLOAD attribute? For example:
<body onload="RunMyJS()">
 
T

Thom Little

If "RunMyJS" is on the server then I believe what you are looking for is the
heavily used Page_Load event handler. Something like ..

private void Page_Load( object sender, System.EventArgs e )
{
if ( ! Page.IsPostBack )
RunMyJS( );
}

If it on the Client then modify the <body> statement the old way.
 
S

Sergey

kw said:
In a server control, is there any way to access the PAGE and modify the BODY
element ONLOAD attribute? For example:
<body onload="RunMyJS()">

As I understood you've just wanted to run some script when you control
has been loaded on page.
Sometime I use Page.RegisterClientScriptBlock on server side (it is
quite simple way)
or I solve this problem in more sophisticated way using behaviour with
event ondocumentready like this:
---- mycontrol.cs
mycontrol.Style.Add("BEHAVIOUR", "url(behaviourPath.htc)"));
------
---------- behaviourPath.htc
<PUBLIC:COMPONENT>
<PUBLIC:ATTACH EVENT="ondocumentready" ONEVENT="RunMyJS()"
</PUBLIC:COMPONENT>
<SCRIPT LANGUAGE='JSCRIPT'>
function RunMyJS()
{
}
</SCRIPT>
----------
 
K

kw

Thanks Thom, but this won't quite work. Code is executed on the client. I
can use RegisterStartupScript to set the add the script to the page, but
still can't modify the body element from a server control (you can't just
edit the page because the server control is a compiled app).
 
K

kw

Yes, I use Page.RegisterClientScriptBlock to set the script but that won't
cause the script to execute on <body onload="runMyJS();">. As for using a
Behavior HTC, I was hoping to avoid doing that because of server control
distribution issues.

Isn't there a simple way to access the page body element and add the onload
attribute (from an asp.net server control)?
 
S

Scott G.

How about this <body id="Body" runat="server" > ; just treat the <body> element as just another HTML tag.

Then in your PreRender (or before Render) on the Page; Body.Attributes["onload"] = "RunMyJS()"

Scott
Yes, I use Page.RegisterClientScriptBlock to set the script but that won't
cause the script to execute on <body onload="runMyJS();">. As for using a
Behavior HTC, I was hoping to avoid doing that because of server control
distribution issues.

Isn't there a simple way to access the page body element and add the onload
attribute (from an asp.net server control)?
 
K

kw

Thanks Scott, but this is an ASP.NET Server control (inherits WebControl). The control doesn't 'know' what page will be using it.
How about this <body id="Body" runat="server" > ; just treat the <body> element as just another HTML tag.

Then in your PreRender (or before Render) on the Page; Body.Attributes["onload"] = "RunMyJS()"

Scott
Yes, I use Page.RegisterClientScriptBlock to set the script but that won't
cause the script to execute on <body onload="runMyJS();">. As for using a
Behavior HTC, I was hoping to avoid doing that because of server control
distribution issues.

Isn't there a simple way to access the page body element and add the onload
attribute (from an asp.net server control)?
 
K

kw

I figured it out.

Because both browsers execute script not in a function on the body load,
there was no need to access the onload element of the body element.

So the correct solution is simply something like this:
<script>
Initialize();
function Initialize(){...
 

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,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top