Click here to Skip to main content
15,893,668 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 171.1K   8K   51  
Async Progress Indicator with Messages and OnComplete Event
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace hmlib.Web.UI.Controls
{
	public partial class ProgressBar
	{
		[Browsable(true)]
		[Category("Behavior")]
		[Description("Indicates Progress ID")]
		public string ProgressId
		{
			get
			{
				if (Attributes["ProgressId"] == null)
				{
					Attributes["ProgressId"] = Guid.NewGuid().ToString();
				}
				return (string)Attributes["ProgressId"];
			}
			set
			{
				Attributes["ProgressId"] = value.ToString();
			}
		}
		[Browsable(true)]
		[Category("Behavior")]
		[Description("Indicates the Url which providing service is located.")]
		[UrlProperty]
		public string ServicePath
		{
			get
			{
				object obj = Attributes["ServicePath"];
				if (obj == null) return Page.Request.Url.AbsolutePath;
				return (string)obj;
			}
			set { Attributes["ServicePath"] = value.ToString(); }
		}
		[Browsable(true)]
		[DefaultValue("getProgress")]
		[Category("Behavior")]
		[Description("Indicates Method Name returning progress status")]
		public string Service_GetMethod
		{
			get
			{
				object obj = Attributes["Service_GetMethod"];
				if (obj == null) return "getProgress";
				return (string)obj;
			}
			set { Attributes["Service_GetMethod"] = value.ToString(); }
		}
		//[Browsable(true)]
		//[DefaultValue("startProgress")]
		//[Category("Behavior")]
		//[Description("Indicates Method name starting the progress")]
		//public string Service_StartMethod
		//{
		//  get
		//  {
		//    object obj = Attributes["Service_StartMethod"];
		//    if (obj == null) return "startProgress";
		//    return (string)obj;
		//  }
		//  set { Attributes["Service_StartMethod"] = value.ToString(); }
		//}
		//[Browsable(true)]
		//[DefaultValue("stopProgress")]
		//[Category("Behavior")]
		//[Description("Indicates Method name starting the progress")]
		//public string Service_StopMethod
		//{
		//  get
		//  {
		//    object obj = Attributes["Service_StopMethod"];
		//    if (obj == null) return "stopProgress";
		//    return (string)obj;
		//  }
		//  set { Attributes["Service_StopMethod"] = value.ToString(); }
		//}
		[Browsable(true)]
		[DefaultValue(1000)]
		[Category("Behavior")]
		[Description("Indicates Interval in milliseconds")]
		public int Interval
		{
			get
			{
				if (Attributes["Interval"] == null)
				{
					Attributes["Interval"] = 1000.ToString();
				}
				return int.Parse(Attributes["Interval"]);
			}
			set
			{
				Attributes["Interval"] = value.ToString();
			}
		}
		[Browsable(true)]
		[Category("Appearance")]
		[Description("Indicates Progress Bar Color")]
		[DefaultValue(typeof(Color), "LightGreen")]
		public Color ProgressColor
		{
			get
			{
				object obj = ViewState["ProgressColor"];
				if (obj == null) obj = Color.LightGreen;
				return (Color)obj;
			}
			set
			{
				ViewState["ProgressColor"] = value;
			}
		}
		[Browsable(true)]
		[DefaultValue("")]
		[Category("Behavior")]
		[Description("Indicates the Message Target")]
		public string MessageTargetId
		{
			get
			{
				object obj = ViewState["MessageTargetId"];
				if (obj == null) return "";
				return (string)obj;
			}
			set { ViewState["MessageTargetId"] = value; }
		}
		[Browsable(true)]
		[Category("Behavior")]
		[Description("")]
		[DefaultValue("")]
		public string ProgressControlId
		{
			get
			{
				if (ViewState["ProgressControlId"] == null) return "";
				return (string)ViewState["ProgressControlId"];
			}
			set
			{
				ViewState["ProgressControlId"] = value;
			}
		}
		[Browsable(true)]
		[Category("Behavior")]
		[Description("Indicates whether to show only the last line of the messages or not")]
		[DefaultValue(false)]
		public bool SingleLine
		{
			get
			{
				if (ViewState["SingleLine"] == null) return false;
				return (bool)ViewState["SingleLine"];
			}
			set
			{
				ViewState["SingleLine"] = value;
			}
		}
		//---------------------------
		private Control MessageTarget
		{
			get
			{
				if (String.IsNullOrWhiteSpace(this.MessageTargetId)) return null;
				Control result = this.Parent.FindControl(this.MessageTargetId);
				if (result == null) throw new Exception("Invalid MessageTargetId '" + this.MessageTargetId + "'");
				if (!IsValidMessageTargetType(result)) throw new NotSupportedException("MessageTarget Type Not Supported");
				return result;
			}
		}
		private Control ProgressControl
		{
			get
			{
				if (String.IsNullOrWhiteSpace(this.ProgressControlId)) return null;
				Control result = this.Parent.FindControl(this.ProgressControlId);
				if (result == null) throw new Exception("Invalid ProgressControlId '" + this.ProgressControlId + "'");
				return result;
			}
		}
		private static bool IsValidMessageTargetType(Control control)
		{
			Type type = control.GetType();
			if (type == typeof(TextBox)) return true;
			if (type == typeof(Label)) return true;
			if (type == typeof(Panel)) return true;
			//if (type == typeof(ListBox)) return true;
			return false;
		}
	}
}

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