473,397 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,397 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 22897

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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.