Executing Javascript on Page Load

Discussion in 'ASP .Net' started by Jonathan Wood, Feb 14, 2008.

  1. I've created some javascript and added it to my page (from a user control)
    using Page.ClientScript.RegisterClientScriptBlock. The javascript hides and
    shows certain elements in response to radio buttons being selected.

    It seems to work fine except that I really need to run the script when the
    page loads so the required elements are shown or hidden (some of these
    elements do not have runat="server").

    So, I use the following code:

    Page.Form.Attributes.Add("onload", initFunction);

    Where initFunction is something like "OnUserFood()".

    This seems to have no effect. And looking at the resulting HTML, I notice
    that my script appears within the form instead of in the header. I'm not
    sure if this matters. But, although the onload attribute correctly appears
    in the form tag, again, it doesn't appear to be actually making the call.

    Any suggestions?

    Thanks.

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com
    Jonathan Wood, Feb 14, 2008
    #1
    1. Advertising

  2. "Jonathan Wood" <> wrote in message
    news:...
    > I've created some javascript and added it to my page (from a user control)
    > using Page.ClientScript.RegisterClientScriptBlock. The javascript hides
    > and shows certain elements in response to radio buttons being selected.
    >
    > It seems to work fine except that I really need to run the script when the
    > page loads so the required elements are shown or hidden (some of these
    > elements do not have runat="server").
    >
    > So, I use the following code:
    >
    > Page.Form.Attributes.Add("onload", initFunction);
    >
    > Where initFunction is something like "OnUserFood()".
    >
    > This seems to have no effect. And looking at the resulting HTML, I notice
    > that my script appears within the form instead of in the header. I'm not
    > sure if this matters. But, although the onload attribute correctly appears
    > in the form tag, again, it doesn't appear to be actually making the call.
    >
    > Any suggestions?


    Page.ClientScript.RegisterClientStartupScript
    Scott Roberts, Feb 14, 2008
    #2
    1. Advertising

  3. Err... That would make sense, wouldn't it?

    Haven't used that before but will investigate further.

    Thanks!

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com

    "Scott Roberts" <-webworks-software.com> wrote in
    message news:...
    >
    > "Jonathan Wood" <> wrote in message
    > news:...
    >> I've created some javascript and added it to my page (from a user
    >> control) using Page.ClientScript.RegisterClientScriptBlock. The
    >> javascript hides and shows certain elements in response to radio buttons
    >> being selected.
    >>
    >> It seems to work fine except that I really need to run the script when
    >> the page loads so the required elements are shown or hidden (some of
    >> these elements do not have runat="server").
    >>
    >> So, I use the following code:
    >>
    >> Page.Form.Attributes.Add("onload", initFunction);
    >>
    >> Where initFunction is something like "OnUserFood()".
    >>
    >> This seems to have no effect. And looking at the resulting HTML, I notice
    >> that my script appears within the form instead of in the header. I'm not
    >> sure if this matters. But, although the onload attribute correctly
    >> appears in the form tag, again, it doesn't appear to be actually making
    >> the call.
    >>
    >> Any suggestions?

    >
    > Page.ClientScript.RegisterClientStartupScript
    >
    Jonathan Wood, Feb 14, 2008
    #3
    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. mart

    Page-load executing twice

    mart, Aug 27, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    1,191
    nfedin
    Aug 27, 2003
  2. venky

    Page Load and webcontrol page load

    venky, Jul 6, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    3,101
    venky
    Jul 11, 2005
  3. Replies:
    2
    Views:
    583
  4. Matcon
    Replies:
    3
    Views:
    21,574
    Matcon
    May 28, 2008
  5. Eric Livingston

    Executing client script before page load

    Eric Livingston, Jan 23, 2008, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    818
    Eric Livingston
    Jan 23, 2008
Loading...

Share This Page