Solved

Stopping server side button click event from client side

Posted on 2008-06-18
7
1,445 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…

730 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