Solved

Stopping server side button click event from client side

Posted on 2008-06-18
7
1,442 Views
Last Modified: 2010-10-05
Hi

I have a server side button control. I want to show an Ok-cancel message from client side when button is clicked, server side event should be handled according to that.

Here is my button code.
<asp:Button ID="btnTest" runat="server" Text="Test Only" onclick="btnTest_Click" OnClientClick="ClientFun()"/>

I dont want the server side event btnTest_Click to happen if user clicks cancel in the client function ClientFun.
Is this possible? Or is there any way to achieve this?

Thanks,
Shiju
0
Comment
Question by:Shiju Sasidharan
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 16

Expert Comment

by:ororiole
ID: 21818448
Yep. Try this:

function ClientFun()
{
  return confirm("Dude, are you sure?");
}
0
 
LVL 14

Author Comment

by:Shiju Sasidharan
ID: 21818485
ororiole,
Thank you for the comment. I tried your code. Stil it is executing server side click event.
My button is inside Update Panel.
0
 
LVL 16

Accepted Solution

by:
ororiole earned 500 total points
ID: 21818512
Change your onclientclick to OnClientClick="javascript:return ClientFun();"
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 21

Expert Comment

by:naspinski
ID: 21818807
or simply inline with OnClientClick="return confirm('Are you sure?')"
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return confirm('Are you sure?')" onclick="Button1_Click" />

Open in new window

0
 
LVL 7

Expert Comment

by:Dauhee
ID: 21820522
Other options won't work with firefox - this definitely works for IE + firefox and probably others

function ClientFun()
{
        if(!confirm("Dude, are you sure?"){
                event.cancelBubble = true;
                event.returnValue = false;
                if(!window.event) document.Form1.addEventListener(""click"", stopEvent, false)
        }else{
                document.Form1.removeEventListener(""click"", stopEvent, false);
        }
}

function stopEvent(event) {
        event.preventDefault();
   }
0
 
LVL 21

Expert Comment

by:naspinski
ID: 21820545
ha, how do you figure they wont work in firefox?  They certainly do :)
0
 
LVL 7

Expert Comment

by:Dauhee
ID: 21820596
I do apologize naspinski I just tested your and ororiole's solution and it certainly works.

The code I posted was a workaround we implemented to stop events bubbling with firefox (can't remember version) some years ago :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question