How to cancel a postback to the server in ASP.NET

Posted by: Suprotim Agarwal , on 6/7/2008, in Category ASP.NET
Views: 100983
Abstract: In this article, we will see how to cancel a postback to the server caused by the button click.
How to cancel a postback to the server in ASP.NET
 
In this article, we will see how to cancel a postback to the server caused by the button click.
ASP.NET provides the OnClientClick property to handle button clicks. You can use this property on Button, LinkButton and ImageButton. The same OnClientClick property also allows you to cancel a postback. Let us quickly see how by first directly using the code in OnClientClick property and then by calling another Javascript function.
Calling code directly
Step 1: Create a web project and drop a Button control on it.
Step 2: Add the OnClientClick property to the button control as shown below and display a confirmation box to the user asking him/her to allow the postback or cancel it.
<asp:Button ID="Button1" runat="server" OnClientClick="return confirm('Are you sure you want to post back to the server?');" Text="Button" />
 
Calling another function
Step 1: Create a web project and drop a Button control on it.
Step 2: Add the OnClientClick property to the button control as shown below, however this time instead of using the code directly, call a javascript function which based on some condition, will return true or false. If the function returns true, the postback occurs. If the function returns false, the postback gets cancelled.
Here’s the complete code:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>My Post Back</title>
    <script type="text/javascript">
    function callPostBack()
    {
        // add any condition here that checks for a value
        if (1 == 2)       
            return true;
        else
            return false;
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" OnClientClick="return callPostBack();" Text="Button" /></div>
    </form>
</body>
</html>
 
The OnClientClick calls the callPostBack function. The callPo stBack() returns false since 1 <> 2 and cancels the postback. You can add your own code in the callPostBack() function to check for a condition before doing the postback.
That’s it. You can use this short snippet to cancel postbacks on button clicks, if need be. I hope this article was useful and I thank you for viewing it.
 If you liked the article,  Subscribe to my RSS Feed. 
 
Give a +1 to this article if you think it was well written. Thanks!
Recommended Articles
Suprotim Agarwal, ASP.NET Architecture MVP, MCSD, MCAD, MCDBA, MCSE, is the CEO of A2Z Knowledge Visuals Pvt. He primarily works as an Architect Consultant and provides consultancy on how to design and develop .NET centric database solutions.

Suprotim is the founder and primary contributor to DotNetCurry, DNC .NET Magazine, SQLServerCurry and DevCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls. and is authoring another one at The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal


Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by Levon Mamikonyan on Wednesday, June 18, 2008 5:09 PM
Another way to do it .... add onsubmit event handler to form .... and again return true/false like for button.
But be careful .. onsubmit also used by validatior controls, so it's better to add event handler from codebehind, using ScriptManager.RegisterOnSubmitStatement, or Page.Form.Attributes.Add("onsubmit", {event handler function})
Difference between this two is first executed before page validators check and the second after page validation (so if page is invalid second code will not be executed.) ......

Happy Coding ... :-D
Comment posted by Suprotim Agarwal on Thursday, June 19, 2008 10:15 AM
Thanks for the tip Levon.
Comment posted by Papachal on Monday, June 23, 2008 1:40 PM
Muy bueno
Comment posted by ashish on Thursday, June 26, 2008 1:12 AM
this is a fantastic article sir
i liked ur way u easily explained
thankyou
Comment posted by saias on Thursday, August 21, 2008 2:10 AM
not worked in IE7
Comment posted by Rama charan on Monday, April 20, 2009 10:41 AM
Thanks a lot for helping us so much with your articles.
Comment posted by Peter The Great on Thursday, May 6, 2010 2:36 PM
I read your code and it saved my day.
Comment posted by Loveleen Saini on Tuesday, June 22, 2010 10:38 AM
Awesome.
It solved my problem.
Thanks..
Comment posted by Jaco on Friday, March 11, 2011 12:07 AM
Thanks for the code, the "return" command was the magic item that I missed.
Comment posted by Tanmoy on Monday, April 4, 2011 4:44 AM
getting this compilation error:
return is not a member of ASP.mypage_aspx

I followed exactly what you wrote here.Plz help.
Comment posted by XCXCX on Thursday, August 18, 2011 7:07 AM
KDFKXXCK
Comment posted by S on Monday, August 27, 2012 4:29 AM
S

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel