How To Handle an async postback timeout in ASP.NET

ASP.NET is an open-source server-side web-application framework designed for web development to produce dynamic web pages developed by Microsoft to allow programmers to build dynamic web sites, applications and services.

In this article, I will show you how to handle Async PostBack in ASP.NET. Check out the following code snippet.

<%@ Page Language="C#" AutoEventWireup="true" %>

<script runat="server">
   protected void slow_postback(object sender, EventArgs e)
   {
       // ten seconds is overkill... we timeout after one second (see ScriptManager below) 
       System.Threading.Thread.Sleep(10000);
   }
</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
   <title>Handling an async postback timeout</title>
</head>
<body>
   <form id="form1" runat="server">
       <!-- This sets the timeout on async postbacks (i.e. UpdatePanel refreshes) to one second -->
       <asp:ScriptManager ID="ScriptManager1" runat="server" AsyncPostBackTimeout="1" />
       <asp:UpdatePanel ID="UpdatePanel1" runat="server">
           <ContentTemplate>
               <asp:Button ID="Button1" runat="server" Text="Fast postback - no timeout" /> <br />
               <asp:Button ID="Button2" runat="server"
                Text="Slow postback - times out after one second"
 OnClick="slow_postback" /> <br />
               Last updated: <%= DateTime.Now %>
           </ContentTemplate>
       </asp:UpdatePanel>
   </form>
</body>

<script type="text/javascript">
   Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function (sender, args) {
       if (args.get_error() && args.get_error().name ===
'Sys.WebForms.PageRequestManagerTimeoutException') {
           alert('Caught a timeout!');
           // remember to set errorHandled = true to keep from getting a
popup from the AJAX library itself 
           args.set_errorHandled(true);
       }
   });
</script>
</html>

إرسال تعليق

Please do not post any spam link in the comment box😊

أحدث أقدم

Blog ads

CodeGuru