To demonstrate this, we will create two UpdatePanels, each displaying the current date and time, and with a button. Each button will update its own UpdatePanel when clicked. However, when the first button is clicked, we will purposely make the process take 3 seconds to complete, giving us enough time to click the second button to test. Upon clicking the second button while the first is still processing, we should be notified that a process is still active and not allow the second process to initiate.
Let's start by adding the ScriptManager and the two UpdatePanels:
Expand|Select|Wrap|Line Numbers
- <form id="form1" runat="server">
- <asp:ScriptManager ID="SM1" runat="server" />
- <asp:UpdatePanel ID="UP1" runat="server" UpdateMode="Conditional">
- <ContentTemplate>
- </ContentTemplate>
- </asp:UpdatePanel>
- <br /><br />
- <asp:UpdatePanel ID="U2" runat="server" UpdateMode="Conditional">
- <ContentTemplate>
- </ContentTemplate>
- </asp:UpdatePanel>
- <br /><br />
- </form>
Let's go ahead and add the buttons and the current time to the UpdatePanels:
Expand|Select|Wrap|Line Numbers
- <asp:UpdatePanel ID="UP1" runat="server" UpdateMode="Conditional">
- <ContentTemplate>
- <%= DateTime.Now.ToString() %>
- <br />
- <asp:Button ID="btn_GetTime1" runat="server" Text="Get Time (1)" OnClick="btn_GetTime1_Click" />
- </ContentTemplate>
- </asp:UpdatePanel>
- <br /><br /> <asp:UpdatePanel ID="U2" runat="server" UpdateMode="Conditional">
- <ContentTemplate>
- <%= DateTime.Now.ToString() %>
- <br />
- <asp:Button ID="btn_GetTime2" runat="server" Text="Get Time (2)" />
- </ContentTemplate>
- </asp:UpdatePanel>
Expand|Select|Wrap|Line Numbers
- using System.Threading;
- ..
- protected void btn_GetTime1_Click(object sender, EventArgs e)
- {
- Thread.Sleep(3000);
- }
Expand|Select|Wrap|Line Numbers
- <asp:UpdateProgress ID="UPr1" runat="server" AssociatedUpdatePanelID="UP1">
- <ProgressTemplate>
- <br /><br />
- Please wait..
- </ProgressTemplate>
- </asp:UpdateProgress>
Expand|Select|Wrap|Line Numbers
- <script type="text/javascript">
- var instance = Sys.WebForms.PageRequestManager.getInstance();
- instance.add_initializeRequest(instance_initializeRequest);
- function instance_initializeRequest(sender, args) {
- if (instance.get_isInAsyncPostBack()) {
- alert('Still processing request. Please wait..');
- args.set_cancel(true);
- }
- }
- </script>
Expand|Select|Wrap|Line Numbers
- <body>
- <form id="form1" runat="server">
- <asp:ScriptManager ID="SM1" runat="server" />
- <asp:UpdatePanel ID="UP1" runat="server" UpdateMode="Conditional">
- <ContentTemplate>
- <%= DateTime.Now.ToString() %>
- <br />
- <asp:Button ID="btn_GetTime1" runat="server" Text="Get Time (1)" OnClick="btn_GetTime1_Click" />
- </ContentTemplate>
- </asp:UpdatePanel>
- <asp:UpdateProgress ID="UPr1" runat="server" AssociatedUpdatePanelID="UP1">
- <ProgressTemplate>
- <br /><br />
- Please wait..
- </ProgressTemplate>
- </asp:UpdateProgress>
- <br /><br />
- <asp:UpdatePanel ID="U2" runat="server" UpdateMode="Conditional">
- <ContentTemplate>
- <%= DateTime.Now.ToString() %>
- <br />
- <asp:Button ID="btn_GetTime2" runat="server" Text="Get Time (2)" />
- </ContentTemplate>
- </asp:UpdatePanel>
- <br /><br />
- </form>
- </body>
- </html>
- <script type="text/javascript">
- var instance = Sys.WebForms.PageRequestManager.getInstance();
- instance.add_initializeRequest(instance_initializeRequest);
- function instance_initializeRequest(sender, args) {
- if (instance.get_isInAsyncPostBack()) {
- alert('Still processing request. Please wait..');
- args.set_cancel(true);
- }
- }
- </script>