Click here to Skip to main content
15,886,026 members
Articles / Web Development / HTML5

AJAX Progress Bar for ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.80/5 (33 votes)
11 Jun 2014CPOL4 min read 170.3K   8K   51  
Async Progress Indicator with Messages and OnComplete Event
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using hmlib.Web.UI.Controls;

namespace WebTest.ProgressBarTest
{
	public partial class ProgressBar_OnCompleteTest : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{

		}
		[WebMethod]
		public static object getProgress(string progressId)
		{
			return Progress.GetResponse(progressId);
		}
		protected void btnStart_Click(object sender, EventArgs e)
		{
			TextBox1.Text = "";
			Progress progress = ProgressBar1.Progress;
			Thread thread = new Thread(() => Start(progress));
			thread.Start();
			btnStart.Enabled = false;
			//ProgressBar1.Completed = false;
		}
		private void Start(Progress progress)
		{
			double max = 100;
			progress.AddMessageLine("Started");
			for (int i = 0; i < max; i++)
			{
				progress.SetProgress(i / max);
				progress.AddMessageLine((i / max) * 100);
				Thread.Sleep(50);
			}
			progress.AddMessageLine("Finished");
			progress.SetProgress(1);
		}
		protected void ProgressBar1_Complete(object sender, EventArgs e)
		{
			btnStart.Enabled = true;
			Label1.Text = DateTime.Now.ToString();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) FDK
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions