Invoking asp:button click in javascript

J

JCE

I need to programmatically invoke an asp:Button click event from a
javascript function. The page containing the script and the button is the
HTML page associated with a WebUserControl-derived object (both the HTML and
control are built in the same .ascx file)

The target button simply fires an event in the WebUserControl-derived
object. So, alternatively, if there was a mechanism to call a method
(explicitly or using an event of some sort) in the WebUserControl-derived
object, from this javascript function - this would achieve the goal too,
with one less degree of indirection..

Seems so simple, but I've searched the newsgroups for hours...
 
C

Chris Jackson

Take a look at the generated html on the client, and reverse engineer what
you need to do. It will emit an <input type="button" /> or <input
type="submit" />. Grab the ID (you can use the UniqueID property of the
Button object to emit this value, which future proofs your app) and then use
javascript to find the control and call the click method.
 
J

JCE

Thank you for your reply.

I think I have tried every permutation of invoking the button click method
in javascript that I can find in the various newsgroups. I always get the
"Page Not Found" error page when the statement executes.

I've used: document.all("Button1").click(); where "Button1" is the ID for
an asp button.

....and document.Form2.Button2.click(); for an HTML INPUT button. (this
statement was built w/ intellisense, so seems legit.)

My app is straightforward, a WebUserControl in an WebForm, all built in .NET
using the visual tools, so don't think I've injected any wierdness into the
situation.

Perhaps you may see something in the syntax above that hints at the problem.
 
Joined
Sep 27, 2006
Messages
1
Reaction score
0
With controls in a user control, the controls ID is has the usercontrols ID appended to it, so you have to search the clientID
 
Joined
Sep 12, 2008
Messages
1
Reaction score
0
ButtonClick

YOu are almost there with
document.all("Button1").click();

However Button1 is what the server knows it as
If you list source on clientside
the client (which is where your javascript is running knows it by a different name
something like ctl00$CP1$Button1

so it is document.all(" ctl00$CP1$Button1").click();
 

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

No members online now.

Forum statistics

Threads
474,264
Messages
2,571,066
Members
48,770
Latest member
ElysaD

Latest Threads

Top