Adding a onload event to the body section.

U

UJ

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 depending on how the page is
loading - with pre-loaded values or just the defaults) So in a sense what I
want to do is something like:

if me.DataType = 0 then
<body onload="LoadRoutine(0)">
else
<body onload="LoadRoutine(1)">
end if

I'm doing stuff in the javascript that I want to do on the client side but
need to set up stuff initially depending on the fields from the server side.

Hope this makes sense.

TIA - Jeffrey.
 
M

Marina

You can dynamically change the name of the method to call.

document.body.onload = Method1

At the top of your page, you can have a conditional, that rewires the event
handler based on something.

The other option, is to have a small server side script inside the
LoadRoutine call, that the server will populate with the correct numeric
value. So when the server is done, it has generated the code correcty so
that the right argument gets passed.
 
T

tomh

I think the easiset way would be to do exactly that....as long as "me"
is public

<% if me.DataType = 0 then %>
<body onload="LoadRoutine(0)">
<% else %>
<body onload="LoadRoutine(1)">
<% end if %>

Not elegant or exacly ASP.NET but it'll work but best way would be
using a page.RegisterClientScriptBlock­()
 
G

Guest

following is what i would do.. (code below is c#)

1 ) Declare a public variable in code behind

public string m_strLoadVariable = "";

2) Assign value to public variable from function / event handlers

if(DataType=0)
m_strLoadVariable = "0";
else
m_strLoadVariable = "1";

3) At client side HTML change body tag to

<body onload="LoadRoutine(<%=m_strLoadVariable%>)">


Other option is to check if Page.RegisterStartupScript will work for your
scenario

if(DataType=0)
strLoadVariable = "0";
else
strLoadVariable = "1";

StringBuilder strScript = new StringBuilder();
strScript.Append("<script language=JavaScript>");
strScript.Append("LoadRoutine(" + strLoadVariable + ");");
strScript.Append("</script>");
Page.RegisterStartupScript("LoadScript",strScript.ToString());


Pls let me know if any Questions

Sreejith
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top