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)">
    <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. Advertisements

  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.
    Marina, Jul 19, 2005
    1. Advertisements

  3. UJ

    Guest Guest

    Look into page.RegisterClientScriptBlock()
    Guest, Jul 19, 2005
  4. UJ

    tomh Guest

    I think the easiset way would be to do exactly 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
  5. UJ

    Guest 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

    m_strLoadVariable = "0";
    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

    strLoadVariable = "0";
    strLoadVariable = "1";

    StringBuilder strScript = new StringBuilder();
    strScript.Append("<script language=JavaScript>");
    strScript.Append("LoadRoutine(" + strLoadVariable + ");");

    Pls let me know if any Questions

    Guest, Jul 19, 2005
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.