Link to home
Start Free TrialLog in
Avatar of Shiju S
Shiju SFlag for United States of America

asked on

Stopping server side button click event from client side

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
Avatar of ororiole
ororiole
Flag of United States of America image

Yep. Try this:

function ClientFun()
{
  return confirm("Dude, are you sure?");
}
Avatar of Shiju S

ASKER

ororiole,
Thank you for the comment. I tried your code. Stil it is executing server side click event.
My button is inside Update Panel.
ASKER CERTIFIED SOLUTION
Avatar of ororiole
ororiole
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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();
   }
ha, how do you figure they wont work in firefox?  They certainly do :)
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 :)