Click here to Skip to main content
15,886,110 members
Articles / Web Development / ASP.NET
Tip/Trick

Simultanious Async Requests Using Multiple Update Panel

Rate me:
Please Sign up or sign in to vote.
4.00/5 (3 votes)
6 Feb 2010CPOL 17.5K   3  
Hi Everyone,I want to share an experience to deal with the issue of simultaneous asynchronous requests using update panel.I faced an issue while working with multiple update panels on same page.I found that we can't perform simultaneous Asynchronous Requests using multiple update panels...
Hi Everyone,

I want to share an experience to deal with the issue of simultaneous asynchronous requests using update panel.

I faced an issue while working with multiple update panels on same page.I found that we can't perform simultaneous Asynchronous Requests using multiple update panels on same page. When we submit multiple request then last request always wins. It was quite unexpected behavior of update panel and against the Asynchronous programming models.
However, if we don't want 'Last request Win' behavior then we can do that by canceling the request if another request is already in progress.All you need to do is.

1. Handle Sys.WebForms.PageRequestManage's InitalizeRequest Event on client side.
2. Use Sys.WebForms.PageRequestManager.get_isInAsyncPostBack() method to know either another request is in progress or not.
3. Cancel the request if another request is already in progress.

Here is sample code:

ASPX:
XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestAJAXApplication._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 runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>
    
    
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
                <script type="text/javascript">
                    var prm = Sys.WebForms.PageRequestManager.getInstance();
                    prm.add_initializeRequest(initializeRequest);
                    function initializeRequest(sender, args) {
                       
                        if (prm.get_isInAsyncPostBack()) {
                            alert("Another Request is already in progress");
                            args.set_cancel(true);
                        }
                        
                    }
</script>
        <table class="style1">
            <tr>
                <td>
                    <asp:UpdatePanel ID="upOne" runat="server" ChildrenAsTriggers="true">
                    <ContentTemplate>
                    <asp:Label ID="lblOne" runat="server" Text="Label"></asp:Label><br />
                    <asp:Button ID="btnUpdateOne" Text="Update" runat="server"
                            onclick="btnUpdateOne_Click" />
                    </ContentTemplate>
                    </asp:UpdatePanel>
                </td>
            </tr>
            <tr>
                <td>
<asp:UpdatePanel ID="upTwo" runat="server" ChildrenAsTriggers="true">
                    <ContentTemplate>
                    <asp:Label ID="lblTwo" runat="server" Text="Label"></asp:Label><br />
                    <asp:Button ID="btnUpdateTwo" Text="Update" runat="server"
                            onclick="btnUpdateTwo_Click" />
                    </ContentTemplate>
                    </asp:UpdatePanel>
                </td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>



Code Behined:
C#
namespace TestAJAXApplication
{
    public partial class _Default : System.Web.UI.Page
    {
      protected void btnUpdateOne_Click(object sender, EventArgs e)
        {
            System.Threading.Thread.Sleep(2500);
            lblOne.Text = string.Format(&quot;Updates On: {0}&quot;, DateTime.Now.ToShortTimeString());
        }
        protected void btnUpdateTwo_Click(object sender, EventArgs e)
        {
            System.Threading.Thread.Sleep(2500);
            lblTwo.Text = string.Format(&quot;Updates On: {0}&quot;, DateTime.Now.ToShortTimeString());
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Imanami Corporation
Pakistan Pakistan
I am Microsoft Certified Technology Specialist for Web Application Development. I have 4 year experience of Web and Distributed application development.I have considerable experience developing client / server software for major corporate clients using the Windows operating systems and .NET platform ( ASP.NET, C# , VB.NET).I have single and multi-threaded code development experience, as well as experience developing database and enterprise level distributed applications.

Comments and Discussions

 
-- There are no messages in this forum --