Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / July 2005

Tip: Looking for answers? Try searching our database.

Adding a onload event to the body section.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
UJ - 19 Jul 2005 20:50 GMT
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.
Marina - 19 Jul 2005 20:58 GMT
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.

>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
[quoted text clipped - 15 lines]
>
> TIA - Jeffrey.
Brad Roberts - 19 Jul 2005 21:01 GMT
Look into page.RegisterClientScriptBlock()

> 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
[quoted text clipped - 14 lines]
>
> TIA - Jeffrey.
tomh - 19 Jul 2005 21:40 GMT
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­()
Sreejith Ram - 19 Jul 2005 22:15 GMT
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

> 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
[quoted text clipped - 14 lines]
>
> TIA - Jeffrey.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.