Cancel an Asynchronous Postback Request Before Sending it to Server in ASP.Net Ajax
There may be situation where we need to cancel an asynchronous postback based on some calculations. For example, when sending inputs to server on an AJAX request you might need to warn the user if he wants to really post it to the server. This gives an option to cancel some updates to the server if the user feels to cancel it.
The following code snippet will warn the user if the entered price is greater than 10,000. Based on the users input the value will be posted or it will cancel the asynchronous postback.
<script language="JavaScript">
var Page;
var postBackElement;
function pageLoad()
{
Page = Sys.WebForms.PageRequestManager.getInstance();
if(!Page.get_isInAsyncPostBack()){
Page.add_initializeRequest(OnInitializeRequest);
}
}
function OnInitializeRequest(sender, args)
{
var result = true;
var amount = $get("txtRupee").value;
if(amount > 10000)
{
result = confirm('Entered amount is greater than 10000!!!Do you need to update?');
}
args.set_cancel(!result);
}
</script>
|