The behavior of an asynchronous postback is calm similar to a synchronous postback. In an asynchronous model, all events occurring on the server side, as they do in a synchronous model. Microsoft AJAX Library also raises events on the client side. However, when the page is rendered, asynchronous postback makes only the contents of the update panel, while in a synchronous postback, the page is recreated & sent to the browser.
In two of the earlier articles, had shown me how to cancel a synchronous postback using ASP.NET. In this article they will see how to cancel an asynchronous postback.
I suppose you have some basic experience developing ASP.NET Ajax applications and has installed the ASP.NET AJAX Library and ASP.NET Control Toolkit. As of this writing, the version of the toolkit is version 1.0. 20229 (if you are targeting Framework 2.0, ASP.NET AJAX 1.0 and Visual Studio 2005) and Version 3.0.20229 (if targeting. NET Framework 3.5 and Visual Studio 2008). I tested the sample using version 3.0.20229 (orientación. NET Framework 3.5 and Visual Studio 2008), though he's run with the 1.0.20229 version .
The application (Sys.Application) and PageRequestManager (Sys.WebForms.PageRequestManager) classes in the library of AJAX are prominent in fundraising events throughout the customer life cycle of an ASP.NET AJAX Web page. If you have an update panel on your page that can handle postbacks asynchronous event handling exposed by the class' PageRequestManager. Soon we will see how we can use client-class events 'PageRequestManager' to provide added functionality, such as canceling a postback or abort the current postback, etc.
The application (Sys.Application) and PageRequestManager (Sys.WebForms.PageRequestManager) classes in the library of AJAX are prominent in fundraising events throughout the customer life cycle of an ASP.NET AJAX Web page. If you have an update panel on your page that can handle postbacks asynchronous event handling exposed by the class' PageRequestManager. Soon we will see how we can use client-class events 'PageRequestManager' to provide added functionality, such as canceling a postback or abort the current postback, etc.
Step 1: Open Visual Studio > File > New Web Site > ASP.NET Web Site > Choose the location and language and click OK.
Step 2: Drag and drop a ScriptManager and an UpdatePanel to the page. In the update panel, drag and drop three buttons and a label control. The first button will cause the first postback, followed by the second one which will cause the second postback and the final button to abort the current postback. After registering the events, the code will look similar to the following:
- <body>
- <form id="form2" runat="server">
- <asp:ScriptManager ID="ScriptManager1" runat="server">
- </asp:ScriptManager>
- <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
- <ContentTemplate>
- <asp:Button runat="server" Text="PostBackFirst" ID="btnPostF" onclick="btnPostF_Click"/>
- <asp:Button runat="server" Text="PostBackSecond" ID="btnPostS" onclick="btnPostS_Click"/>
- <asp:Button runat="server" Text="AbortPostBack" ID="btnAbort"/>
- <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
- </ContentTemplate>
- </asp:UpdatePanel>
- </form>
- </body>
- In the event handlers, add the following code:
- C#
- protected void btnPostF_Click(object sender, EventArgs e)
- {
- System.Threading.Thread.Sleep(4000);
- Label1.Text = "PostBack 1 Completed";
- }
- protected void btnPostS_Click(object sender, EventArgs e)
- {
- System.Threading.Thread.Sleep(4000);
- Label1.Text = "PostBack 2 Completed";
- }
- VB.NET
- Protected Sub btnPostF_Click(ByVal sender As Object, ByVal e As EventArgs)
- System.Threading.Thread.Sleep(4000)
- Label1.Text = "PostBack 1 Completed"
- End Sub
- Protected Sub btnPostS_Click(ByVal sender As Object, ByVal e As EventArgs)
- System.Threading.Thread.Sleep(4000)
- Label1.Text = "PostBack 2 Completed"
- End Sub
Here, we are introducing a delay of 4 seconds when a button click occurs. You can replace this with your processing code, like retrieving results from a database or a similar operation.
Step 3: To the <head> element on the page, add the following javascript code:
- <script type="text/javascript">
- function pageLoad()
- {
- Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(cancelPostBack);
- }
- function cancelPostBack(sender, args)
- {
- if (Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack())
- {
- alert('One postback at a time please');
- args.set_cancel(true);
- }
- }
- </script>
As already explained in the introduction of this article, the code above demonstrates how to use and register the ‘initializeRequest’ event to determine whether an asynchronous postback is currently executing by using the ‘isInAsyncPostBack’ property. You can also cancel the current request by using the cancel property of the Sys.CancelEventArgs class.
Note: You can get the ID of the element which has initiated a new postback by ‘postBackElement’ property of the ‘Sys.WebForms.InitializeRequestEventArgs’ class. Something similar to: var ctrl = args.get_postBackElement() which returns the postback element that initiated the asynchronous postback.
Step 4: The last step left is to build functionality to abort the current postback by using the ‘abortPostBack()’ of the ‘PageRequestManager’ class. To do so, add the following code to the onClientClick() of the btnAbort as shown below:
- <asp:Button runat="server" Text="AbortPostBack" ID="btnAbort"
- OnClientClick="Sys.WebForms.PageRequestManager.getInstance().abortPostBack();
- alert('Postback Cancelled');"/>
- The entire source will look similar to the following:
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>Cancel Async PostBack</title>
- <script type="text/javascript">
- function pageLoad()
- {
- Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(cancelPostBack);
- }
- function cancelPostBack(sender, args)
- {
- if (Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack())
- {
- alert('One postback at a time please');
- args.set_cancel(true);
- }
- }
- </script>
- </head>
- <body>
- <form id="form2" runat="server">
- <asp:ScriptManager ID="ScriptManager1" runat="server">
- </asp:ScriptManager>
- <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
- <ContentTemplate>
- <asp:Button runat="server" Text="PostBackFirst" ID="btnPostF"
- onclick="btnPostF_Click"/>
- <asp:Button runat="server" Text="PostBackSecond" ID="btnPostS"
- onclick="btnPostS_Click"/>
- <asp:Button runat="server" Text="AbortPostBack" ID="btnAbort"
- OnClientClick="Sys.WebForms.PageRequestManager.getInstance().abortPostBack();
- alert('Postback Cancelled');"/>
- <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
- </ContentTemplate>
- </asp:UpdatePanel>
- </form>
- </body>
- </html>
In order to test the functionality, follow these steps:
1. Run the application.
2. Click on ‘PostBackFirst’. Now immediately click on PostBackSecond’. You will receive a message ‘One postback at a time please’. So our script has successfully discovered that there is an async postback currently in progress and hence does not allow the other.
3. Now to check how to abort a current async postback, click on either the ‘PostBackFirst’ or on PostBackSecond’ and wait for 4 seconds. The default behavior is that when the current thread wakes up (remember we did Thread.Sleep()), a message is displayed ‘PostBack Completed’. Now click on the button again and this time, click on the ‘AbortPostBack’ button. You will see that the postback has aborted and the ‘PostBack Completed’ message is never displayed.
0 comments:
Post a Comment