Programmatically click asp.net button via javascript?

C

Cathryn Johns

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.AllocationsForm.LoadButton.click()
complains that document.AllocationsFrom.LoadButton is null or doesn't
exist, and __doPostBack('LoadButton', '') does a postback but doesn't
actually execute the method.
 
E

Eliyahu Goldin

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
 
I

intrader

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.AllocationsForm.LoadButton.click()
complains that document.AllocationsFrom.LoadButton is null or doesn't
exist, and __doPostBack('LoadButton', '') does a postback but doesn't
actually execute the method.

Candidate for Ajax?
 
C

Cathryn Johns

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?
 
G

Guest

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.
 
C

CJ

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.
 

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

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top