?
Solved

Stopping server side button click event from client side

Posted on 2008-06-18
7
Medium Priority
?
1,461 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 total points
ID: 21818512
Change your onclientclick to OnClientClick="javascript:return ClientFun();"
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
Suggested Courses

762 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