This is probably a really simple thing. Basically, the user clicks a button and a potentially long running task happens. I'd like to do a few things like toggle the button's enabled state, show a spinner, etc. In VB.NET Winforms I'd just do Application.DoEvents() and the updates would happen and the code can continue. How can I do this in ASP.NET? (preferable serverside or minimal javascript)
There are a few ways to approach this in ASP.Net depending on exactly what your requirement is. Getting the process started is fairly easy, but monitoring it for completion from the client side will require more work in both the server and the client.
The basic outline of the solution is:
1) Perform some action on the client that initiates the action. For example, you could post the entire page back on a button click, initiate an ajax request, or have a partial page postback depending on how much information you need from the page.
2) On the server side, initiate the task using a BackgroundWorker, make an entry in a workflow queue, or store a request in a database table that is monitored by a service that is responsible for performing the action.
3) Back on the client side, use javascript start a window.timeout loop that, when it times out, issues an ajax request to the web server to check on the completion. Using a timeout loop like this will ensure that the UI remains responsive and that any animations being displayed will display correctly. How you check on the completion will depend on how your server-side implementation is designed, but will almost certainly require a database.
We use the following general approach for initiating reports from the web client, which can be long running:
When the user initiates the report, open a new window to the report generation page on the client using javascript, passing the page enough parameters to get it started. Opening a separate window allows the user to continue working, but still see that there is something happening.
The user interface for the report page basically contains an animated GIF so that the user knows that something is going on.
When the report page is initially loaded on the server, it generates a unique id for monitoring the status of the report and embeds this in javascript for use in monitoring the status. It then stores this unique identifier in a database table that contains the unique id and a status column, initializing the status to requested.
Once the database entry has been made, the page fires off a BackgroundWorker to initiate the action and then returns the page to the user.
When the page is displayed, javascript starts a window.timeout loop that periodically fires off an ajax request to the web server, which then checks the database for the status of the report using the unique identifier created earlier.
When the backgroundworker finishes the report, either successfully or in failure, it updates the database table with the status, location of the report or error messages and terminates.
When the javascript loop finds that the report generation has completed, it either displays the error message or the report to the user.
Hopefully, this gives you some ideas for solving your issue.
The issue with this could be that once the page is posting you can't update other sections of the page.
you can use multiple asp:updatepanel and communicate to other update panel's causing the state to change in the panel.
take a look at this link:
http://www.ajaxtutorials.com/ajax-tutorials/tutorials-using-multiple-updatepanels-in-net-3-5-and-vb/
it will show you how to accomplish this.
Related
I am using asp.net and Ajax functionalities to make requests to server via web services for a search functionality. On click of search button, I am making the screen inactive by displaying a loading image over it. The issue is some searches takes longer time, and the user is forced to wait till the results are fetched. Is there any way the search can be cancelled manually, like a button click while the search is being performed by the server.
Any help or alternates will be appreciated.
One thing you can try is to set timeout on your ajax call, take a look here:asp-net-page-webmethod-ajax-call-request-timed-out
Another thing is to use javascript setTimeOut to display the user a message after a few seconds saying "its taking too long. wait for results or Abort?" where the "Abort" is a link that refreshes the page.
In our web application, a user can make a change that requires a lot of database tables to update. The load time for all that can be up to 30 seconds. I don't want the user to wait for that to complete before navigating to another page.
I've put the long-running code on its own page (say, "updateinfo.aspx") and tried a few solutions, including jQuery AJAX calls to "updateinfo.aspx" or loading an image file that calls "updateinfo.aspx". In all cases, I cannot navigate from the original HTML page that kicked off the AJAX call to another HTML page while "updateinfo.aspx" is executing. Chrome says that the request to "updateinfo.aspx" is pending. When I click on a link to navigate away from original HTML page, we're "Waiting for example.org..." until the AJAX page is finished, then the request to navigate to the next HTML page follows through and the new page loads.
So, this defeats the purpose of putting the long-running code into an AJAX page. The user's page renders quickly, but they cannot continue about their day by navigating to another page until the AJAX page is finished. I don't care about the output of the AJAX page.
Any thoughts?
You shouldn't really execute a long-running process in a web page context; the HTTP Request/Response model is not favourable to that concept when the client application is a web browser. This is a scenario I have had to address a number of times; you could: -
use MSMQ; submit a message to a queue containing details of the operation to be performed, or
write the details of the operation to be performed to a "Jobs" table
You can then create a Windows Service to read messages from the queue/pull un-processed items one at a time from the table, and perform the long-running operation.
In the most recent project I had to do this, from memory, I created a usercontrol that sat in the header (i.e. in the masterpage) which polled the database table via jQuery Ajax once every 15 seconds to detect when the job was completed, and show a popup to the user indicating the job was finished.
I can try and dig out some examples somewhere but those are the main moving parts, does that help at all ?
I was wondering if there is any way to always run some server side code when a user leaves a page in ASP.NET. The page Unload event is no good because that doesn't get called if someone clicks on a link. Ideally I'd also like the code to run even if the user closes the browser.
I suspect what I'm asking isn't possible, but it doesn't hurt to ask
Problem is, HTTP is a stateless protocol, so when the page has finished being served, you wont know if the user is still on the page or not.
The only way to acheive this would be a hidden piece of Javascript that constantly pings the server with it's session ID, or another similar mechanism. When the ping becomes unresponsive you can reasonably assume the page is not being viewed by the user anymore.
Here is a diagram that explains traditional HTTP message flow.
im not really sure if you can do that but i have a workaround in mind.
There is an event in the DOM called onbeforeunload. it get calls everytime a user leaves a page. you can try sending an ajax request to the server from this function.
The closest thing you can come without creating too messy a solution is to enable ASP sessions. This will create a session on the server for each visitor, who will be identified by a cookie.
After a certain amount of inactivity from the visitor, the session will be closed, and a SessionEnd event will be raised. This you can hook up to in the Global.asax file.
I will not recommend this however, because HTTP is pr. definition a session-less protocol, and using server based sessions violates this fact, and are often problematic. Many solutions that use server based sessions run into problems when the user uses the browser-back button, and resubmits a form. Because the content of the submitted form no longer corresponds the data that exists in the server session.
Also, enabling server based sessions seriously hurts the scalability of the application.
Not that I know of. You'll need to use javascript for that, and call a web service on the server side.
I'm having a bit of trouble cancelling an asynch postback. I have an update panel with an update progress which contains a cancel button so that the user can cancel the postback. When the user clicks a button to generate a report the update progress is shown. The report can take a bit of time as it has to loop through a thousand or so times creating an excel spreadsheet. If the user decides to cancel running the report for any reason then they can click the cancel button which I then call abortPostBack() in javascript which stops the update progress and the page is shown again. However, the user can't do anything else like navigate to another page as the server is still processing the loop. How would I stop the loop on the server processing when the user has clicked the cancel button? Any help appreciated!
Are you saying that a simple HTTP link is not accessible on the client side until the async postback is complete? If so, that sounds like a conundrum, since you either have to optimize your server side process, or set a smaller server-side request timeout. Either that or redesign your user-interaction to make the server side Excel generation process an asynchronous one, rather than synchronous, so that the user doesn't have to wait until the Excel generation is complete. You could fancy this up on the client side to then set a JavaScript timer to periodically query the server to see if the file was ready, and if so, indicate that to the user with and give them a download file link option, or something.
Otherwise, if you could invoke another AJAX request while waiting on that to return (which you may not to from the sound of it), you could simply perform a new HTTP request that "cancels" the long running process. But that seems like it would not work since the server is still handling the long running HTTP request. So I'd opt to investigate the options in my first paragraph.
If cancelling did allow an async HTTP request to be performed on the client side, then you could set a session state value to indicate that cancel was requested. Personally I wouldn't approach it this way. But if you did, then your long-running server-side process could periodically look for the existence of a session value. Say:
if (Session["cancel-me"] != null)
{
Session["cancel-me"] = null;
abortThisLongProcess();
}
Yep, even if you navigate away from the page using the browser back button, as soon as you click anything else that needs to post back to the server the page hangs until the long process has completed. Looks like there's no way of canceling so I will have to look at redesigning the Excel generation.
I haven't found a way to cancel the request that's running, but there's no real reason that you can't start a new one.
By default ASP.Net tries (it can't always) to apply an exclusive lock on the session object - as soon as one page reads it every other page request that passes the same session ID (by cookie or on the URL) has to wait for the first page to release the session.
It doesn't matter that the client has cancelled the request - the server will continue to lock the session until the original page finishes executing.
I think the solution is to do away with the ASP session entirely. Then when the user requests another page it begins immediately, even though the server is still processing the old request on another thread.
I have a asp .net master page application and one content page has a number of controls on it.
I want to store the content/state of those controls in the session state whenever a user navigates to another content page.
My question is, how do I know when to capture the control state? Is there an event of some type I can use to trigger a procedure?
Thanks in advance for any info.
Bill
There aren't any real "global" server side event handlers that you can use to detect when the user is going to another page (or even just hitting the back button in the browser).
The best bet is to simply write a method in your master page that will save your session state and then execute a Response.Redirect() to the location specified. Then make all of your links go through this method when you need to track session state.
Presumably they are navigating away from that page via a control that you have provided them (button, link, etc.) You can trap the action on the server side at that point and cache your state.
What about using session variables?
See this link for more info:
ASP.Net Session
There's a number of ways to maintain state. There are trade-offs no matter which version you use. For example, I wouldn't use session variables on an app that is load-balanced across multiple servers; you're not guaranteed to get the same server for each request, and the state is stored on a server-by-server basis.
It's hard to beat this
Session["myState"] = 7; // bad example
for simplicity, though. :)