Run Javascript function upon Page Load from Page Load event handle

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?TWF0Y29u?=

    #1

    Run Javascript function upon Page Load from Page Load event handle

    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 <headof 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.Attribu tes.Add("onChan ge", "javascript:myf unction()")

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

    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.
  • Stan

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

    On 27 May, 23:29, Matcon <Mat...@discuss ions.microsoft. comwrote:
    Hello,
    >
    I have a asp.net 2.0 (vb.net) page, which uses a master page, and there isa
    javascript function (myfunction) defined in the <headof 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.Attribu tes.Add("onChan ge", "javascript:myf unction()")
    >
    However, the following gives me an error that "Attributes is not a member of
    pagename":
    Me.Attributes.A dd("onLoad", "javascript:myF unction()")
    >
    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?

    Comment

    • Mark Rae [MVP]

      #3
      Re: Run Javascript function upon Page Load from Page Load event handle

      "Matcon" <Matcon@discuss ions.microsoft. comwrote in message
      news:B9579642-D654-41FC-937A-358E792543EE@mi crosoft.com...
      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 <headof 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.Attribu tes.Add("onChan ge", "javascript:myf unction()")
      OK. FYI, the "javascript :" part is unnecessary...
      However, the following gives me an error that "Attributes is not a member
      of
      pagename":
      Me.Attributes.A dd("onLoad", "javascript:myF unction()")
      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 <bodytag as follows:

      <body id="objMasterBo dy" runat="server">

      2) Add the following to your content page's Page_Load event:

      Master.FindCont rol("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="myfunct ion();">


      --
      Mark Rae
      ASP.NET MVP


      Comment

      • =?Utf-8?B?TWF0Y29u?=

        #4
        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:

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

        Thoughts on what I should try?


        "Mark Rae [MVP]" wrote:
        "Matcon" <Matcon@discuss ions.microsoft. comwrote in message
        news:B9579642-D654-41FC-937A-358E792543EE@mi crosoft.com...
        >
        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 <headof 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.Attribu tes.Add("onChan ge", "javascript:myf unction()")
        >
        OK. FYI, the "javascript :" part is unnecessary...
        >
        However, the following gives me an error that "Attributes is not a member
        of
        pagename":
        Me.Attributes.A dd("onLoad", "javascript:myF unction()")
        >
        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 <bodytag as follows:
        >
        <body id="objMasterBo dy" runat="server">
        >
        2) Add the following to your content page's Page_Load event:
        >
        Master.FindCont rol("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="myfunct ion();">
        >
        >
        --
        Mark Rae
        ASP.NET MVP

        >
        >

        Comment

        • Mark Rae [MVP]

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

          "Matcon" <Matcon@discuss ions.microsoft. comwrote in message
          news:7B8A6A0C-2967-4A83-B58C-73836196A331@mi crosoft.com...

          [top-posting corrected]
          >2) Add the following to your content page's Page_Load event:
          >>
          >Master.FindCon trol("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:
          >
          "'Attribute s' is not a member of 'System.Web.UI. Control'."
          >
          Thoughts on what I should try?
          Apologies - my fault.

          Try something like this:

          DirectCast(Mast er.FindControl( "objBody"),
          HtmlGenericCont rol).Attributes .Add("onload", "alert('Hello') ;")

          I think that should work...


          --
          Mark Rae
          ASP.NET MVP


          Comment

          • =?Utf-8?B?TWF0Y29u?=

            #6
            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" <Matcon@discuss ions.microsoft. comwrote in message
            news:7B8A6A0C-2967-4A83-B58C-73836196A331@mi crosoft.com...
            >
            [top-posting corrected]
            >
            2) Add the following to your content page's Page_Load event:
            >
            Master.FindCont rol("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:

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

            Thoughts on what I should try?
            >
            Apologies - my fault.
            >
            Try something like this:
            >
            DirectCast(Mast er.FindControl( "objBody"),
            HtmlGenericCont rol).Attributes .Add("onload", "alert('Hello') ;")
            >
            I think that should work...
            >
            >
            --
            Mark Rae
            ASP.NET MVP

            >
            >

            Comment

            Working...