Run client-side JavaScript when a asp:Button is clicked

Run client-side JavaScript when a asp:Button is clicked

Post by Andy Oak » Fri, 18 Apr 2003 00:29:52


I have an <asp:button> called cmdDeleteUser which I use to delete user
records from a database. Ideally what I want to happen is for the
browser to fire off a javascript:window.confirm("Are you sure you wish
to delete this user?") and only trigger the server-side event
cmdDeleteUser_click if the user selects Yes.

Any ideas?

 
 
 

Run client-side JavaScript when a asp:Button is clicked

Post by paul crowde » Fri, 18 Apr 2003 03:36:29


Put this in your CodeBehind:

cmdDeleteUser.Attributes.Add("onclick", "return(confirm('Are you sure you
wish to delete this user?'))");

This will add a client-side onclick attribute.

    Paul


Quote:> I have an <asp:button> called cmdDeleteUser which I use to delete user
> records from a database. Ideally what I want to happen is for the
> browser to fire off a javascript:window.confirm("Are you sure you wish
> to delete this user?") and only trigger the server-side event
> cmdDeleteUser_click if the user selects Yes.

> Any ideas?


 
 
 

Run client-side JavaScript when a asp:Button is clicked

Post by Kathlee » Sat, 19 Apr 2003 00:01:48


Andy & Paul,

One problem :  this won't work if you have any validators on the page.  At
least, after hours of frustration I still can't make it work.

Part of the reason it took me so long to confirm this was that I couldn't
get it to work even if I commented out the validators!  I used <!--  ... -->
to comment out the validators for testing, and validator JavaScript was
still rendered, even after a fresh build.

If I completely deleted the validators from the .aspx page, the onclick
worked.  I discovered this when I broke up the tag with spaces ( "as  p:req
uiredfieldvalidator") to make sure it wasn't being read while commented out,
and I got complaints from the compiler in vs, so I deleted the line
altogether.  Just that change made it work.

Am I missing something here?  Is there anyway to add the onclick attribute
to an asp:button control and have it work, even when the validators are
present?   I really miss being able to do this.

Kathleen


> Put this in your CodeBehind:

> cmdDeleteUser.Attributes.Add("onclick", "return(confirm('Are you sure you
> wish to delete this user?'))");

> This will add a client-side onclick attribute.

>     Paul



> > I have an <asp:button> called cmdDeleteUser which I use to delete user
> > records from a database. Ideally what I want to happen is for the
> > browser to fire off a javascript:window.confirm("Are you sure you wish
> > to delete this user?") and only trigger the server-side event
> > cmdDeleteUser_click if the user selects Yes.

> > Any ideas?

 
 
 

Run client-side JavaScript when a asp:Button is clicked

Post by Daniel Lemir » Sat, 19 Apr 2003 00:03:33


You must set the button to not cause any validation or postback.

Daniel

 
 
 

Run client-side JavaScript when a asp:Button is clicked

Post by Kathlee » Sat, 19 Apr 2003 00:25:00


Actually I find I can have it do javascript and a postback without problem.
It will run the javascript that was added to it's attributes with the
buttonName.Attributes.Add  function first, followed by the buttonName_Click
code in the code behind.   It's just the validator routines that it
conflicts with, because both are trying to connect javascript.

Since you can have mutiple validators on a page, or even on a single control
(I have a requiredFieldValidator and a rangeValidator on a textbox, for
example), the framework is able to add multiple javascript functions to the
button onclick without problem.  I was just hoping it could add my function
as well as the one's it generates itself.  Apparently not.


Quote:> You must set the button to not cause any validation or postback.

> Daniel

 
 
 

Run client-side JavaScript when a asp:Button is clicked

Post by Daniel Lemir » Sat, 19 Apr 2003 00:39:19


If you want your javascript AND the validations to be performed I suggest to
drop the attribute and cheat a little bit.

You can cheat by adding a customvalidator and do your javascript in there. But
it must be the first validator defined in the html code. For the return value I
suggest you take a look at the customvalidator in the help files.

Daniel

 
 
 

Run client-side JavaScript when a asp:Button is clicked

Post by Kathlee » Sat, 19 Apr 2003 00:59:32


Excellent idea!!!  Thanks!

Kathleen

Quote:> If you want your javascript AND the validations to be performed I suggest
to
> drop the attribute and cheat a little bit.

> You can cheat by adding a customvalidator and do your javascript in there.
But
> it must be the first validator defined in the html code. For the return
value I
> suggest you take a look at the customvalidator in the help files.

> Daniel

 
 
 

Run client-side JavaScript when a asp:Button is clicked

Post by Kathlee » Mon, 21 Apr 2003 04:09:18


Just wanted to confirm that this works like a charm.

Thanks again Daniel!

Kathleen


Quote:> If you want your javascript AND the validations to be performed I suggest
to
> drop the attribute and cheat a little bit.

> You can cheat by adding a customvalidator and do your javascript in there.
But
> it must be the first validator defined in the html code. For the return
value I
> suggest you take a look at the customvalidator in the help files.

> Daniel

 
 
 

1. How do I call a server-side click event after client-side Javascript runs?

I have a Web form that I want to runs some checks on using JavaScript and if
the script passes, then I want to call the Click event of the button.

I have tried using the following code:
nextButton.Attributes.Add("onclick", "submitPage();");
nextButton.Click += new EventHandler(nextButton_Click);

This calls the JavaScript function (submitPage), but it calls the Click
event even if the script fails.  Is there a way to call the Server-side
click event from javascript?  Or, is there a way to disable the Click-Event?
I guess an alternative would be to just submit the form using javascript and
handle the post-back in the page load, but I was hoping that there is a more
elegant way to handle this.

2. ActiveX controls with ASP.NET

3. Disable asp:Button on click event - client and server side event processing

4. I just won a amiga 500 hard drive off ebay, is it gona be a pain in the ass to set up

5. How do you refer to an HTML server-ran control in client-side javascript?

6. Re Re: credit where credit is due

7. Run client-side javascript

8. Automatic "Fwd:" in subject line?

9. How do I run client-side javascript with a .NET web control???

10. How could I call server-side function from client-side Javascript?

11. How to call a server side-script when user clicks client-side menuitem ?

12. debugging client-side Javascript file in ASP.NET app?

13. run JavaScript from Button.Click, How?