Wednesday, August 29, 2012

Asp.Net Button OnClick Vs OnClientClick


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 will be executed first, followed by the OnClick event handler.

The
OnClientClick event handler can be used to perform client side validations before the page is submitted, the OnClick event handler can be used to perform server side operations like saving data to database.

Here is an example using both the events.

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>OnClientClick</title>
    <script language="javascript">
        function JavascriptFunction() {
            alert("Hi! I am a Javascript Function");
        }
    </script>
</head>
<body>
    <form id="frmOnClientClick" runat="server">
    <div>
<asp:Button
            id="btnSave"
            runat="server"
            Text="Save"
            OnClientClick="JavascriptFunction();"/>
    </div>
    </form>
</body>
</html>

Here when the user clicks on the button the client side JavaScript function JavascriptFunction() will be called first, following which the server side function btnSave_Click will be executed.

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

1 comment:

Web Development Neumarkt in der Oberpfalz said...

There is no call to the server side in that example.