change <body> tag from Page_Load event?

S

SteveS

Hello, I'm having trouble figuring out how to dynamically add an onload and
resize event to the <body> tag. I've looked on the web and in MS
documentation unsuccessfuly. Does anyone know the code to do this? The end
result would be something like this:

<body onload="MyFunction()" resize="ResizeFunction()">

Your help is greately appreciated!!!
 
S

Scott Allen

Yes, here is one possible solution:

Give your body tag the runat and id attributes:

<body runat="server" id="Body">

Then from code behind, obtain a reference to the control and make it
sumbit to your wishes:

HtmlGenericControl body = FindControl("Body") as HtmlGenericControl;
if (body != null)
{
body.Attributes["onload"] = "MyFunction();";
}

HTH,
 
C

Curt_C [MVP]

try adding a runat=server and declaring it in the codebehind, then you can
do what you want with it
 
E

Eliyahu Goldin

If you make protected properties onLoadHandler and onResizeHandler that will
hold the text you want to use in onload and resize, you can simply write

<body onload="<%=onLoadHandler %>" resize="<%=onResizeHandler %>">

Eliyahu
 
A

Aquila Deus

Curt_C said:
try adding a runat=server and declaring it in the codebehind, then you can
do what you want with it

Heh, it would be cool if ASP.NET allows events fired on client-side to
post back directly, something like:
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top