Cancel AJAX Requests with the AjaxManager or AjaxPanel
Environment
Product | Progress® Telerik® UI for ASP.NET AJAX AjaxManager | Progress® Telerik® UI for ASP.NET AJAX AjaxPanel |
Description
How can I cancel an Ajax request with the Telerik UI for ASP.NET AjaxManager or AjaxPanel?
Solution
To cancel an AJAX request, in the OnRequestStart
client-side event handler of the AjaxManager or AjaxPanel, call the set_cancel(true)
to prevent the request from continuing.
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
<script type='text/javascript'>
function OnRequestStart(ajaxPanel, eventArgs) {
var eventTarget = eventArgs.get_eventTarget();
if (eventTarget == "<%= Button1.UniqueID %>") {
if (!CheckZipCode()) {
eventArgs.set_enableAjax(false); // cancel the ajax request
}
}
}
function CheckZipCode() {
var zipCode = $get('<%= TextBox1.ClientID %>').value;
if (zipCode.length != 5) {
alert('Please enter a valid 5 digit postal code!');
return false;
}
else {
var fiveDigitCheckRE = /^\d{5}$/ //regular expression for checking a 5 digit number
if (zipCode.search(fiveDigitCheckRE) == -1) {
alert("Only digits are allowed!");
return false;
}
}
return true;
}
</script>
</telerik:RadCodeBlock>