Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 807
Phillip Schmidt Avatar asked Mar 05 '26 22:03

Phillip Schmidt


1 Answers

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.

like image 200
Servy Avatar answered Mar 08 '26 11:03

Servy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!