Run Javascript function upon Page Load from Page Load event handle

Discussion in 'ASP .Net' started by Matcon, May 27, 2008.

  1. Matcon

    Matcon Guest

    Hello,

    I have a asp.net 2.0 (vb.net) page, which uses a master page, and there is a
    javascript function (myfunction) defined in the <head> of that master page.
    Is there a way to run the function from the page load event of the page?

    I am able to run the function when there is a change in, say, a text box, by
    adding the following to the page load event:
    TextBox.Attributes.Add("onChange", "javascript:myfunction()")

    However, the following gives me an error that "Attributes is not a member of
    pagename":
    Me.Attributes.Add("onLoad", "javascript:myFunction()")

    I'm trying to get myfunction to run when the page loads by adding the
    correct code to Me.Load.

    Thank you for your help.
    Matcon, May 27, 2008
    #1
    1. Advertising

  2. Matcon

    Stan Guest

    Re: Run Javascript function upon Page Load from Page Load eventhandle

    On 27 May, 23:29, Matcon <> wrote:
    > Hello,
    >
    > I have a asp.net 2.0 (vb.net) page, which uses a master page, and there is a
    > javascript function (myfunction) defined in the <head> of that master page..  
    > Is there a way to run the function from the page load event of the page?
    >
    > I am able to run the function when there is a change in, say, a text box, by
    > adding the following to the page load event:
    > TextBox.Attributes.Add("onChange", "javascript:myfunction()")
    >
    > However, the following gives me an error that "Attributes is not a member of
    > pagename":
    > Me.Attributes.Add("onLoad", "javascript:myFunction()")
    >
    > I'm trying to get myfunction to run when the page loads by adding the
    > correct code to Me.Load.
    >
    > Thank you for your help.


    Are you confusing the server-side PageLoad event with the client-side
    OnLoad event?

    The PageLoad event is handled by the server. Any JavaScript contained
    in the page file is passed transparently back to the client. Niether
    IIS or the ASP.NET worker process understand JavaScript and cannot
    meaningfully execute it during the rendering of the page.

    Am I making any sense?
    Stan, May 28, 2008
    #2
    1. Advertising

  3. Matcon

    Matcon Guest

    Re: Run Javascript function upon Page Load from Page Load event ha

    Thanks for that information - I think I'm close. I followed step 1 and 2,
    but I'm getting an error that reads:

    "'Attributes' is not a member of 'System.Web.UI.Control'."

    Thoughts on what I should try?


    "Mark Rae [MVP]" wrote:

    > "Matcon" <> wrote in message
    > news:...
    >
    > > I have a asp.net 2.0 (vb.net) page, which uses a master page, and there is
    > > a
    > > javascript function (myfunction) defined in the <head> of that master
    > > page.

    >
    > OK.
    >
    > > Is there a way to run the function from the page load event of the page?

    >
    > Yes.
    >
    > > I am able to run the function when there is a change in, say, a text box,
    > > by
    > > adding the following to the page load event:
    > > TextBox.Attributes.Add("onChange", "javascript:myfunction()")

    >
    > OK. FYI, the "javascript:" part is unnecessary...
    >
    > > However, the following gives me an error that "Attributes is not a member
    > > of
    > > pagename":
    > > Me.Attributes.Add("onLoad", "javascript:myFunction()")

    >
    > Yes, it would do.
    >
    > [Also, bear in mind that JavaScript is case-sensitive e.g. myfunction() is
    > not the same as myFunction()...]
    >
    > > I'm trying to get myfunction to run when the page loads by adding the
    > > correct code to Me.Load.

    >
    > 1) Modify the MasterPage's <body> tag as follows:
    >
    > <body id="objMasterBody" runat="server">
    >
    > 2) Add the following to your content page's Page_Load event:
    >
    > Master.FindControl("objBody").Attributes.Add("onload", "myfunction();")
    >
    >
    > Alternatively, if you want the function to run for every content page which
    > uses this MasterPage, then just do:
    >
    > <body onload="myfunction();">
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    >
    >
    Matcon, May 28, 2008
    #3
  4. Matcon

    Matcon Guest

    Re: Run Javascript function upon Page Load from Page Load event ha

    That worked! Thanks very much for your help.


    "Mark Rae [MVP]" wrote:

    > "Matcon" <> wrote in message
    > news:...
    >
    > [top-posting corrected]
    >
    > >> 2) Add the following to your content page's Page_Load event:
    > >>
    > >> Master.FindControl("objBody").Attributes.Add("onload", "myfunction();")

    > >
    > > Thanks for that information - I think I'm close. I followed step 1 and 2,
    > > but I'm getting an error that reads:
    > >
    > > "'Attributes' is not a member of 'System.Web.UI.Control'."
    > >
    > > Thoughts on what I should try?

    >
    > Apologies - my fault.
    >
    > Try something like this:
    >
    > DirectCast(Master.FindControl("objBody"),
    > HtmlGenericControl).Attributes.Add("onload", "alert('Hello');")
    >
    > I think that should work...
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    >
    >
    Matcon, May 28, 2008
    #4
    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. john
    Replies:
    3
    Views:
    9,215
  2. Jon Perez
    Replies:
    0
    Views:
    275
    Jon Perez
    Jun 6, 2004
  3. glorfindel
    Replies:
    1
    Views:
    116
    Samuel Kim
    Feb 19, 2005
  4. Patient Guy

    No Body Node Upon Page Load?

    Patient Guy, Nov 8, 2005, in forum: Javascript
    Replies:
    5
    Views:
    133
    Julian Turner
    Nov 8, 2005
  5. TheBelangerFamily
    Replies:
    0
    Views:
    111
    TheBelangerFamily
    Mar 21, 2006
Loading...

Share This Page