RadAjax for ASP.NET

capture connection error and cancel ajax request Send comments on this topic.
How-to > capture connection error and cancel ajax request

Glossary Item Box

RadAjax does not provide out-of-the-box support for detecting connection timeout errors. However, here is a code snippet that demonstrates how you could achieve the desired functionality through the client-side API of RadAjax:

JavaScript Copy Code
<script type="text/javascript">
var timeoutId = null;
function OnRequestStart(sender, args)
{
    // set your desired timeout value here in milliseconds
    var connectionTimeout = 3000; // 3sec

    // get the XMLHttpRequest object
    var requestInProgress = args.XMLHttpRequest;

    // get the loading panel
    var currentLoadingPanel = RadAjaxNamespace.LoadingPanels["<%= LoadingPanel1.ClientID %>"];

    // get the updated control ID
    var currentUpdatedControl = "<%= RadAjaxPanel1.ClientID %>";

    // abort the request and hide the loading panel if the Ajax Request has not returned prior
    // to the evaluation of the expression (that occurs after the specified number of milliseconds)
    timeoutId = window.setTimeout(function()
                                    {
                                        requestInProgress.abort();
                                        currentLoadingPanel.Hide(currentUpdatedControl);
                                        alert("Connection timeout!");
                                    }, connectionTimeout);
}

function OnResponseEnd(sender, args)
{
    // clear the timeout if the request returns successfully
    window.clearTimeout(timeoutId);
}
</script>

 

ASPX Copy Code
<rad:RadAjaxPanel ID="RadAjaxPanel1" Height="100px" Width="200px" runat="server"
   
ClientEvents-OnResponseEnd="OnResponseEnd" ClientEvents-OnRequestStart="OnRequestStart"
   
LoadingPanelID="LoadingPanel1">
   
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
   
<asp:Label ID="Label1" runat="server">
   
</asp:Label>
</
rad:RadAjaxPanel>
<
rad:AjaxLoadingPanel ID="LoadingPanel1" runat="server" Transparency="10" BackColor="Beige">
   
<asp:Image ID="Image1" runat="server" ImageUrl="~/RadControls/Ajax/Skins/Default/loading.gif" />
</
rad:AjaxLoadingPanel>
C# Copy Code
protected void Button1_Click(object sender, EventArgs e)
{
   
//this is to simulate 5 seconds delay from the server and is only added for the example. In your real
   
//life page you do not need this!!!

   
System.Threading.Thread.Sleep(5000);
   Label1.Text = DateTime.Now.ToString();
}
 

 When you use this approach for RadControls, you need to invoke the currentLoadingPanel.Hide() method with a slight change due to the rendering of the RadControls. The code that shows the loading panels tries to append "_wrapper" at the end of the updated controls, because all of the RadControls' html wrapper elements end in this suffix. However, the code for hiding the loading panel will not look for the wrapper suffix, which will cause a javascript error. To use the above script for RadControls please apply the following change to the Hide() method:

currentLoadingPanel.Hide(currentUpdatedControl+"_wrapper");