Solved

Stopping server side button click event from client side

Posted on 2008-06-18
7
1,437 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now