472,776 Members | 2,527 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 472,776 developers and data experts.

How To Use AJAX To Detect If An Asynchronous Request Is Being Processed

85 64KB
In this article, you will learn how to use AJAX to detect if an Asynchronous request is still being processed when another is called. By doing this, we can notify the user, and allow the first request to complete before starting another.
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
  1. <form id="form1" runat="server">
  2. <asp:ScriptManager ID="SM1" runat="server" />
  3.  
  4. <asp:UpdatePanel ID="UP1" runat="server" UpdateMode="Conditional">
  5. <ContentTemplate>
  6.  
  7. </ContentTemplate>
  8. </asp:UpdatePanel>
  9.  
  10. <br /><br />
  11. <asp:UpdatePanel ID="U2" runat="server" UpdateMode="Conditional">
  12. <ContentTemplate>
  13.  
  14. </ContentTemplate>
  15. </asp:UpdatePanel>
  16. <br /><br />
  17. </form>
  18.  
Notice we set the UpdateMode on both Panels to Conditional. This is because we do not want them both updating at the same time - each button should update their own UpdatePanel only.
Let's go ahead and add the buttons and the current time to the UpdatePanels:
Expand|Select|Wrap|Line Numbers
  1. <asp:UpdatePanel ID="UP1" runat="server" UpdateMode="Conditional">
  2. <ContentTemplate>
  3. <%= DateTime.Now.ToString() %>
  4. <br />
  5. <asp:Button ID="btn_GetTime1" runat="server" Text="Get Time (1)" OnClick="btn_GetTime1_Click" />
  6. </ContentTemplate>
  7. </asp:UpdatePanel>
  8. <br /><br /> <asp:UpdatePanel ID="U2" runat="server" UpdateMode="Conditional">
  9. <ContentTemplate>
  10. <%= DateTime.Now.ToString() %>
  11. <br />
  12. <asp:Button ID="btn_GetTime2" runat="server" Text="Get Time (2)" />
  13. </ContentTemplate>
  14. </asp:UpdatePanel>
  15.  
now in the code behind , in the click event of first button, we will deliberately make the thread sleep for 3 seconds. like this

Expand|Select|Wrap|Line Numbers
  1. using System.Threading;
  2.  
  3. ..
  4.  
  5. protected void btn_GetTime1_Click(object sender, EventArgs e)
  6. {
  7. Thread.Sleep(3000);
  8. }
  9.  
To make the request more visual, let's add an UpdateProgress into the mix as well. This will show us when a request is in progress:
Expand|Select|Wrap|Line Numbers
  1. <asp:UpdateProgress ID="UPr1" runat="server" AssociatedUpdatePanelID="UP1">
  2. <ProgressTemplate>
  3. <br /><br />
  4. Please wait..
  5. </ProgressTemplate>
  6. </asp:UpdateProgress>
  7.  
We will also include some JavaScript to check if an Async PostBack is currently in progress. To do this, add the following script:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. var instance = Sys.WebForms.PageRequestManager.getInstance();
  3. instance.add_initializeRequest(instance_initializeRequest);
  4.  
  5. function instance_initializeRequest(sender, args) {
  6. if (instance.get_isInAsyncPostBack()) {
  7. alert('Still processing request. Please wait..');
  8. args.set_cancel(true);
  9. }
  10. }
  11. </script>
  12.  
The ASPX page will look something like this:
Expand|Select|Wrap|Line Numbers
  1. <body>
  2. <form id="form1" runat="server">
  3. <asp:ScriptManager ID="SM1" runat="server" />
  4.  
  5. <asp:UpdatePanel ID="UP1" runat="server" UpdateMode="Conditional">
  6. <ContentTemplate>
  7. <%= DateTime.Now.ToString() %>
  8. <br />
  9. <asp:Button ID="btn_GetTime1" runat="server" Text="Get Time (1)" OnClick="btn_GetTime1_Click" />
  10. </ContentTemplate>
  11. </asp:UpdatePanel>
  12. <asp:UpdateProgress ID="UPr1" runat="server" AssociatedUpdatePanelID="UP1">
  13. <ProgressTemplate>
  14. <br /><br />
  15. Please wait..
  16. </ProgressTemplate>
  17. </asp:UpdateProgress>
  18. <br /><br />
  19. <asp:UpdatePanel ID="U2" runat="server" UpdateMode="Conditional">
  20. <ContentTemplate>
  21. <%= DateTime.Now.ToString() %>
  22. <br />
  23. <asp:Button ID="btn_GetTime2" runat="server" Text="Get Time (2)" />
  24. </ContentTemplate>
  25. </asp:UpdatePanel>
  26. <br /><br />
  27. </form>
  28. </body>
  29. </html>
  30. <script type="text/javascript">
  31. var instance = Sys.WebForms.PageRequestManager.getInstance();
  32. instance.add_initializeRequest(instance_initializeRequest);
  33.  
  34. function instance_initializeRequest(sender, args) {
  35. if (instance.get_isInAsyncPostBack()) {
  36. alert('Still processing request. Please wait..');
  37. args.set_cancel(true);
  38. }
  39. }
  40. </script>
  41.  
Aug 1 '12 #1
0 22798

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Lars | last post by:
I am trying to post a pretty long string (9300 bytes) in a hidden field and i get this very strange error message: Request object, ASP 0107 (0x800A01B8) The data being processed is over the...
0
by: mrwoopey | last post by:
Hi, My OLAP data cube is giving me the following error when I am manipulating OLAP data cube views: "the data being processed is over the allowed limit" I know that this message is caused by...
1
by: monomaniac21 | last post by:
hi all! how can i enter html and php into a db and display this within a TEXTAREA without it being processed as code? do i need to replace characters like <? with something else like say <..? or...
2
by: Alan Johnson | last post by:
FAQ 17.3 says: "never throw an exception from a destructor while processing another exception" What mechanism is available for me to know if an exception is being processed? I imagine this...
4
by: Olivier Matrot | last post by:
Hello, I have a problem with an ASP.NET 2.0 Application. A client request is processed in parrallel by two threads. This ends with the following exception : <Source>System</Source> <StackTrace...
1
by: Olivier Matrot | last post by:
I have a problem with an ASP.NET 2.0 Application. A client request is processed in parrallel by two threads. After further analysis, it appears that : - There is 2 disctinct session for the...
1
by: Geoff Cox | last post by:
Hello I have a file called fs-test.txt which has in it 7/7/2008 9/7/2008 and a pl file use warnings;
0
Frinavale
by: Frinavale | last post by:
I have a peculiar problem... Background: I have a function that I don't want the user to execute more than once while they are waiting for it to process; therefore, I disable all of the...
3
Markus
by: Markus | last post by:
I've changed between XAMPP and EasyPHP, and the error occurs on both. I know the PHP is being processed because if I do something like a header() redirect, it'll redirect to the page, but then...
1
Frinavale
by: Frinavale | last post by:
I don't have a clue how to work around this problem and I'm open to any suggestions on the topic.... In Internet Explorer it appears that the onbeforeunload event is fireing despite the fact that...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.