How to cancel a postback to the server in ASP.NET
Posted by: Suprotim Agarwal ,
on 6/7/2008,
in
Category ASP.NET
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.
This article has been editorially reviewed by Suprotim Agarwal.
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!
Was this article worth reading? Share it with fellow developers too. Thanks!
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