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

Posted by: Suprotim Agarwal , on 6/7/2008, in Category ASP.NET
Views: 139040
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. 
 

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for Sixteen consecutive years. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.

Get in touch with him on Twitter @suprotimagarwal or at LinkedIn



Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
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
Comment posted by geg on Wednesday, April 15, 2015 6:15 AM
rgeg