⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pleasewaitbutton.cs

📁 ASP C#代码实例 适合初学人士学习使用
💻 CS
字号:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;


namespace JavaScriptControls
{
	/// <summary>
	/// Summary description for WebCustomControl1.
	/// </summary>
	[DefaultProperty("Text"),
	ToolboxData("<{0}:PleaseWaitButton runat=server></{0}:PleaseWaitButton>")]
	public class PleaseWaitButton : Button
	{
		private string _pleaseWaitText = "请等待…… ";
		private string _pleaseWaitImage = String.Empty;
		private PleaseWaitTypeEnum _pleaseWaitType = PleaseWaitTypeEnum.TextThenImage;

		[Bindable(true),
		Category("Appearance"),
		DefaultValue("请等待…… ")]
		public string PleaseWaitText
		{
			get
			{
				return _pleaseWaitText;
			}

			set
			{
				_pleaseWaitText = value;
			}
		}

		[Bindable(true),
		Category("Appearance"),
		DefaultValue("")]
		public string PleaseWaitImage
		{
			get
			{
				return _pleaseWaitImage;
			}

			set
			{
				_pleaseWaitImage = value;
			}
		}

		[Bindable(true),
		Category("Appearance"),
		DefaultValue(PleaseWaitTypeEnum.TextThenImage)]
		public PleaseWaitTypeEnum PleaseWaitType
		{
			get
			{
				return _pleaseWaitType;
			}

			set
			{
				_pleaseWaitType = value;
			}
		}

		
		private string GetEmbeddedTextFile(string sTextFile)
		{
			// generic function for retrieving the contents
			// of an embedded text file resource as a string

			// we'll get the executing assembly, and derive
			// the namespace using the first type in the assembly
			Assembly a = Assembly.GetExecutingAssembly();
			String sNamespace = a.GetTypes()[0].Namespace;

			// with the assembly and namespace, we'll get the
			// embedded resource as a stream
			Stream s = a.GetManifestResourceStream(
						string.Format("{0}.{1}", sNamespace, sTextFile)
					);
			
			// read the contents of the stream into a string
			StreamReader sr = new StreamReader(s);
			String sContents = sr.ReadToEnd();

			sr.Close();
			s.Close();

			return sContents;
		}


		private void RegisterJavascriptFromResource()
		{
			// load the embedded text file "javascript.txt"
			// and register its contents as client-side script
			string sScript = GetEmbeddedTextFile("javascript.txt");
			this.Page.RegisterClientScriptBlock("PleaseWaitButtonScript", sScript);
		}

		private void RegisterJavascriptPreloadImage(string sImage)
		{
			Regex rex = new Regex("[^a-zA-Z0-9]");
			string sImgName = "img_" + rex.Replace(sImage, "_"); 

			StringBuilder sb = new StringBuilder();
			sb.Append("<script language='JavaScript'>");
			sb.Append("if (document.images) { ");
			sb.AppendFormat("{0} = new Image();", sImgName);
			sb.AppendFormat("{0}.src = \"{1}\";", sImgName, sImage);
			sb.Append(" } ");
			sb.Append("</script>");

			this.Page.RegisterClientScriptBlock(sImgName + "_PreloadScript", sb.ToString());
		}


		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			// the client-side javascript code is kept
			// in an embedded resource; load the script
			// and register it with the page.
			RegisterJavascriptFromResource();
		}

		private string GetControlHtml(Control c)
		{
			StringWriter sw = new StringWriter();
			HtmlTextWriter writer = new HtmlTextWriter(sw);
			c.RenderControl(writer);
			string sHtml = sw.ToString();
			writer.Close();
			sw.Close();

			return sHtml;
		}


		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender (e);

			// If we're using an image, register some javascript
			// for client-side image preloading
			if (_pleaseWaitImage != String.Empty && _pleaseWaitType != PleaseWaitTypeEnum.TextOnly)
				RegisterJavascriptPreloadImage(_pleaseWaitImage);
		}


		private bool IsValidatorIncludeScript()
		{
			// return TRUE if this page has registered javascript
			// for client-side validation; this code may not be registered
			// if ASP.NET detects what it thinks (correctly or incorrectly)
			// is a down-level browser.

			return this.Page.IsStartupScriptRegistered("ValidatorIncludeScript");
		}

		
		private string GeneratePleaseWaitJavascript()
		{
			// create a JavaScript "PleaseWait()" function call
			// suitable for use in an onclick event handler

			string sMessage = "";
			string sText = _pleaseWaitText;
			string sImage = (_pleaseWaitImage != String.Empty 
				? string.Format(
				"<img src=\"{0}\" align=\"absmiddle\" alt=\"{1}\"/>"
				, _pleaseWaitImage, _pleaseWaitText )
				: String.Empty);

			// establish the layout based on PleaseWaitType
			switch (_pleaseWaitType)
			{
				case PleaseWaitTypeEnum.TextThenImage:
					sMessage = sText + sImage;
					break;
				case PleaseWaitTypeEnum.ImageThenText:
					sMessage = sImage + sText;
					break;
				case PleaseWaitTypeEnum.TextOnly:
					sMessage = sText;
					break;
				case PleaseWaitTypeEnum.ImageOnly:
					sMessage = sImage;
					break;
			}

			// return the final code chunk
			string sCode = string.Format(
				"PleaseWait('pleaseWaitButtonDiv_{0}', 'pleaseWaitButtonDiv2_{1}', '{2}');"
				, this.ClientID, this.ClientID, sMessage);
			sCode = sCode.Replace("\"", "&quot;");

			return sCode;
		}


		private string ModifyJavaScriptOnClick(string sHtml)
		{
			// given the rendered html for a Button,
			// modify the onclick attribute (or add one)
			// to include our javascript function call to PleaseWait();
			// Normally, we would think to do this by overriding
			// AddAttributesToRender() and modifying the 
			// onclick attribute that way.  However, if client-side
			// validation is present, we need to ensure that our
			// PleaseWait() function is executed <<after>> the client-side
			// validation function.  If we follow conventional means for
			// adding our onclick handler in AddAttributesToRender, or
			// OnPreRender, the code to execute client-side validation
			// would be the last function in onclick added to the HtmlTextWriter stream
			// Doing it this hacked way lets us insert our code after
			// the client-side validation code.
			//
			// Thanks to CodeProject member KJELLSJ (Kjell-Sverre Jerijaervi)
			// for code ideas to allow the button to work with client-side validation

			string sReturn = "";
			string sPleaseWaitCode = GeneratePleaseWaitJavascript();

			// is there an existing onclick attribute?
			Regex rOnclick = new Regex("onclick=\"(?<onclick>[^\"]*)");
			Match mOnclick = rOnclick.Match(sHtml);
			if (mOnclick.Success)
			{
				// there is an existing onclick attribute;
				// add our code to the end of it; if client-side
				// validation has been rendered, make sure
				// we check to see if the page is valid;
				string sExisting = mOnclick.Groups["onclick"].Value;
				string sReplace = sExisting 
					     + (sExisting.Trim().EndsWith(";") ? "" : "; ");
				
				if (IsValidatorIncludeScript() && this.CausesValidation)
				{
					// include code to check if the page is valid
					string sCode = "if (Page_IsValid) " + sPleaseWaitCode + " return Page_IsValid;";
					// add our code to the end of the existing onclick code;
					sReplace = sReplace + sCode;
				}
				else
				{
					// don't worry about the page being valid;
					sReplace = sReplace + sPleaseWaitCode;
				}

				// now substitute our onclick code
				sReplace = "onclick=\"" + sReplace;
				sReturn = rOnclick.Replace(sHtml, sReplace);
			}
			else
			{
				// there isn't an existing onclick attribute;
				// add ours
				int i = sHtml.Trim().Length - 2;
				string sInsert = " onclick=\"" + sPleaseWaitCode + "\" ";
				sReturn = sHtml.Insert(i, sInsert);				
			}
			
			return sReturn;

		}


		/// <summary>
		/// Render this control to the output parameter specified.
		/// </summary>
		/// <param name="output"> The HTML writer to write out to </param>
		protected override void Render(HtmlTextWriter output)
		{
			// What a hack... but this helps to support browsers
			// with client-side validation script as well as those
			// without.  Output the button's html (with attributes)
			// to a dummy HtmlTextWriter
			StringWriter sw = new StringWriter();
			HtmlTextWriter wr = new HtmlTextWriter(sw);
			base.Render(wr);
			string sButtonHtml = sw.ToString();
			wr.Close();
			sw.Close();
			// now modify the code to include an "onclick" handler
			// with our PleaseWait() function called appropriately
			// after any client-side validation.
			sButtonHtml = ModifyJavaScriptOnClick(sButtonHtml);
			

			// before rendering the button, output an empty <div>
			// that will be populated client-side via javascript
			// with a "please wait" message"
			output.Write(string.Format("<div id='pleaseWaitButtonDiv2_{0}'>", this.ClientID));
			output.Write("</div>");

			// render the button in an encapsulating <div> tag of its own
			output.Write(string.Format("<div id='pleaseWaitButtonDiv_{0}'>", this.ClientID));
			output.Write(sButtonHtml);
			output.Write("</div>");
		}

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -