简体   繁体   中英

Update progress bar from codebehind

I have a really long submit() -type function on one of my web pages that runs entirely on the server, and I'd like to display a progress bar to the client to show the, well, progress.

I'd be ok with updating it at intervals of like 20% so long as I can show them something.

Is this even possible? Maybe some kind of control with runat="server" ? I'm kind of lost for ideas here.

It's possible , but it's quite a bit harder to do in a web based environment than in, for example, a desktop based environment.

What you'll have to do is submit a request to the server, have the server start the async task and then send a response back to the client. The client will then need to periodically poll the server (likely/ideally using AJAX) for updates. The server will want to, within the long running task's body, set a Session value (or use some other method of storing state) that can be accessed by the client's polling method.

It's nasty, and messy, and inefficient, so you wouldn't want to do this if there are going to be lots of users executing this.

Here is an example implementation by Microsoft. Note that this example uses UpdatePanel objects, ASP timers, etc. which make the code quite a bit simpler to write (and it's still not all that pretty) but these components are fairly "heavy". Using explicity AJAX calls, creating web methods rather than doing full postbacks, etc. will improve the performance quite a bit. As I said though, even in the best of cases, it's a performance nightmare. Don't do this if you have a lot of users or if this is an operation performed very much. If it's just for occasional use by a small percentage of admin users then that may not be a concern, and it does add a lot from the user's perspective.

I would take a look at .net 4.5's async and await.

Using Asynchronous Methods in ASP.NET MVC 4 -- (MVC example I know sorry)

Then check out this example using a progress bar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM