Programmatically click asp.net button via javascript?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cathryn  Johns

    Programmatically click asp.net button via javascript?

    Hi

    I have a server-side (asp.net) method that can be called by clicking a
    <asp:button> on my webpage. This method should also be called every n
    seconds, so I've set up a javascript timer which fires every n seconds.
    The problem is that I then don't know how to call the server-side
    method.

    I figured that since I already have a button calling that method, I
    could just emulate the button click, but I've tried two methods and
    neither seem to work. document.Alloca tionsForm.LoadB utton.click()
    complains that document.Alloca tionsFrom.LoadB utton is null or doesn't
    exist, and __doPostBack('L oadButton', '') does a postback but doesn't
    actually execute the method.

  • Eliyahu Goldin

    #2
    Re: Programmaticall y click asp.net button via javascript?

    In situations like yours I usually introduce a hidden input control for
    passing an action code from client to server side. It is much simpler than
    trying to mimic exact client-side button click handling.

    Eliyahu

    "Cathryn Johns" <cjohns@gmail.c om> wrote in message
    news:1130848841 .282492.173550@ g44g2000cwa.goo glegroups.com.. .[color=blue]
    > Hi
    >
    > I have a server-side (asp.net) method that can be called by clicking a
    > <asp:button> on my webpage. This method should also be called every n
    > seconds, so I've set up a javascript timer which fires every n seconds.
    > The problem is that I then don't know how to call the server-side
    > method.
    >
    > I figured that since I already have a button calling that method, I
    > could just emulate the button click, but I've tried two methods and
    > neither seem to work. document.Alloca tionsForm.LoadB utton.click()
    > complains that document.Alloca tionsFrom.LoadB utton is null or doesn't
    > exist, and __doPostBack('L oadButton', '') does a postback but doesn't
    > actually execute the method.
    >[/color]


    Comment

    • Phillip Williams

      #3
      RE: Programmaticall y click asp.net button via javascript?

      Try:

      var btn = document.getEle mentById(btnNam e);
      if (btn) btn.click();

      as I did in this sample:


      --
      HTH,
      Phillip Williams




      "Cathryn Johns" wrote:
      [color=blue]
      > Hi
      >
      > I have a server-side (asp.net) method that can be called by clicking a
      > <asp:button> on my webpage. This method should also be called every n
      > seconds, so I've set up a javascript timer which fires every n seconds.
      > The problem is that I then don't know how to call the server-side
      > method.
      >
      > I figured that since I already have a button calling that method, I
      > could just emulate the button click, but I've tried two methods and
      > neither seem to work. document.Alloca tionsForm.LoadB utton.click()
      > complains that document.Alloca tionsFrom.LoadB utton is null or doesn't
      > exist, and __doPostBack('L oadButton', '') does a postback but doesn't
      > actually execute the method.
      >
      >[/color]

      Comment

      • intrader

        #4
        Re: Programmaticall y click asp.net button via javascript?

        On Tue, 01 Nov 2005 04:40:41 -0800, Cathryn Johns wrote:
        [color=blue]
        > Hi
        >
        > I have a server-side (asp.net) method that can be called by clicking a
        > <asp:button> on my webpage. This method should also be called every n
        > seconds, so I've set up a javascript timer which fires every n seconds.
        > The problem is that I then don't know how to call the server-side
        > method.
        >
        > I figured that since I already have a button calling that method, I
        > could just emulate the button click, but I've tried two methods and
        > neither seem to work. document.Alloca tionsForm.LoadB utton.click()
        > complains that document.Alloca tionsFrom.LoadB utton is null or doesn't
        > exist, and __doPostBack('L oadButton', '') does a postback but doesn't
        > actually execute the method.[/color]

        Candidate for Ajax?

        Comment

        • Cathryn  Johns

          #5
          Re: Programmaticall y click asp.net button via javascript?

          Nope, that doesn't work either - if (btn) just returns false. I assume
          that this is because in your sample, the button is a regular html
          <input type="button".. .>, whereas mine is an actual asp.net button
          (<asp:button ....>).

          Any ideas on how to click an asp.net button?

          Phillip Williams wrote:[color=blue]
          > Try:
          >
          > var btn = document.getEle mentById(btnNam e);
          > if (btn) btn.click();
          >
          > as I did in this sample:
          >
          > http://www.societopia.net/Samples/asppage_buttons.aspx
          > --
          > HTH,
          > Phillip Williams
          > http://www.societopia.net
          > http://www.webswapp.com
          >
          >
          > "Cathryn Johns" wrote:
          >[color=green]
          > > Hi
          > >
          > > I have a server-side (asp.net) method that can be called by clicking a
          > > <asp:button> on my webpage. This method should also be called every n
          > > seconds, so I've set up a javascript timer which fires every n seconds.
          > > The problem is that I then don't know how to call the server-side
          > > method.
          > >
          > > I figured that since I already have a button calling that method, I
          > > could just emulate the button click, but I've tried two methods and
          > > neither seem to work. document.Alloca tionsForm.LoadB utton.click()
          > > complains that document.Alloca tionsFrom.LoadB utton is null or doesn't
          > > exist, and __doPostBack('L oadButton', '') does a postback but doesn't
          > > actually execute the method.
          > >
          > >[/color][/color]

          Comment

          • Phillip Williams

            #6
            Re: Programmaticall y click asp.net button via javascript?

            Nope :-)

            The buttons in my demo are <asp:button> and they do work. Another demo is
            the City of North Vancouver's website (which I developed) where the search
            button is also an asp:button and working using btn.Click() too.
            --
            HTH,
            Phillip Williams




            "Cathryn Johns" wrote:
            [color=blue]
            > Nope, that doesn't work either - if (btn) just returns false. I assume
            > that this is because in your sample, the button is a regular html
            > <input type="button".. .>, whereas mine is an actual asp.net button
            > (<asp:button ....>).
            >
            > Any ideas on how to click an asp.net button?
            >
            > Phillip Williams wrote:[color=green]
            > > Try:
            > >
            > > var btn = document.getEle mentById(btnNam e);
            > > if (btn) btn.click();
            > >
            > > as I did in this sample:
            > >
            > > http://www.societopia.net/Samples/asppage_buttons.aspx
            > > --
            > > HTH,
            > > Phillip Williams
            > > http://www.societopia.net
            > > http://www.webswapp.com
            > >
            > >
            > > "Cathryn Johns" wrote:
            > >[color=darkred]
            > > > Hi
            > > >
            > > > I have a server-side (asp.net) method that can be called by clicking a
            > > > <asp:button> on my webpage. This method should also be called every n
            > > > seconds, so I've set up a javascript timer which fires every n seconds.
            > > > The problem is that I then don't know how to call the server-side
            > > > method.
            > > >
            > > > I figured that since I already have a button calling that method, I
            > > > could just emulate the button click, but I've tried two methods and
            > > > neither seem to work. document.Alloca tionsForm.LoadB utton.click()
            > > > complains that document.Alloca tionsFrom.LoadB utton is null or doesn't
            > > > exist, and __doPostBack('L oadButton', '') does a postback but doesn't
            > > > actually execute the method.
            > > >
            > > >[/color][/color]
            >
            >[/color]

            Comment

            • CJ

              #7
              Re: Programmaticall y click asp.net button via javascript?

              Found the problem, and you're right, it had nothing to do with asp.net
              buttons :-)

              The button is in a .ascx user control which is sitting in a .aspx page,
              and I had to prefix the button name with the name of the user control
              WebUI element. Once I figured that out, it all worked smoothly.

              Comment

              Working...