Wednesday, August 29, 2012

Asp.net button OnClientClick Confirm Message


The Asp.Net button has 2 attributes to trigger a click event OnClick and OnClientClick.

OnClientClick – Calls the client side (JavaScript) event handler
OnClick – Calls the server side (code-behind) event handler

The
OnClientClick event handler can be used to perform client side validations and to get a confirmation from the user before triggering the server side event handler as follows.

OnClientClick="if(!confirm('Are you sure you want to Save?')) return false;"

Here is a full example

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>OnClientClick</title>
</head>
<body>
    <form id="frmOnClientClick" runat="server">
    <div>
        <asp:Button
            id="btnSave"
            runat="server"
            Text="Client Click"
            OnClientClick="if(!confirm('Are you sure you want to Save?'))  
            return false;"
            onclick="btnSave_Click"/>
    </div>
    </form>
</body>
</html>

Here once the user clicks on the button, a confirmation message 'Are you sure you want to Save?' will get poped up in a message box with, if the user selects Yes then the server side event will be triggered, else the server side event will not be triggered.

Related Post
Asp.Net Button OnClientClick Example
How to: Call JavaScript function on OnClientClick event of Asp.Net button
Asp.Net Button OnClick Vs OnClientClick
Asp.Net Button OnClientClick prevent post back.
Asp.net button OnClientClick Confirm Message




Search Flipkart Products:
Flipkart.com

No comments: