Adding a onload event to the body section.

Discussion in 'ASP .Net' started by UJ, Jul 19, 2005.

  1. UJ

    UJ Guest

    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.
    UJ, Jul 19, 2005
    #1
    1. Advertising

  2. UJ

    Marina Guest

    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.

    "UJ" <> wrote in message
    news:e4E$...
    >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, Jul 19, 2005
    #2
    1. Advertising

  3. Look into page.RegisterClientScriptBlock()

    "UJ" wrote:

    > 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.
    >
    >
    >
    >
    >
    =?Utf-8?B?QnJhZCBSb2JlcnRz?=, Jul 19, 2005
    #3
  4. UJ

    tomh Guest

    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­()
    tomh, Jul 19, 2005
    #4
  5. 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


    "UJ" wrote:

    > 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.
    >
    >
    >
    >
    >
    =?Utf-8?B?U3JlZWppdGggUmFt?=, Jul 19, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. baroque Chou
    Replies:
    1
    Views:
    1,834
    Laurent Bugnion
    Jan 24, 2006
  2. Bender
    Replies:
    2
    Views:
    252
    Thomas 'PointedEars' Lahn
    Oct 22, 2003
  3. David Otton

    window.onload and body.onload differences

    David Otton, Nov 4, 2004, in forum: Javascript
    Replies:
    2
    Views:
    535
    Martin Honnen
    Nov 4, 2004
  4. Replies:
    5
    Views:
    252
    Thomas 'PointedEars' Lahn
    May 15, 2005
  5. Replies:
    2
    Views:
    386
    Richard Cornford
    Sep 12, 2005
Loading...

Share This Page