defaultbuttontextbox.cs

来自「本系统是在asp版《在线文件管理器》的基础上设计制作」· CS 代码 · 共 111 行

CS
111
字号
//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Text;
using System.Web.UI.WebControls;

namespace CommunityServer.Controls
{
	/// <summary>
	/// Provides a simple way to specify a specific button to fire when "Enter" is clicked with in 
	/// the textbox
	/// </summary>
	public class DefaultButtonTextBox : TextBox
	{
		public DefaultButtonTextBox()
		{

		}

		#region DefaultButton
		private WebControl _defaultButton = null;

		/// <summary>
		/// Attempts to find a reference to the Control specified in the Button property
		/// </summary>
		protected WebControl DefaultButton
		{
			get
			{
				if(_defaultButton == null)
				{
					if(Button != null)
					{
						_defaultButton = FindControl(Button) as WebControl;
						if(_defaultButton != null)
						{
							if(!(_defaultButton is Button || _defaultButton is LinkButton || _defaultButton is ImageButton))
								throw new ArgumentException("DefautlButton Property value must be of Type Button, LinkButton, or ImageButton");
						}
					}
				}
				return _defaultButton;
			}
		}

		string _button;
		/// <summary>
		/// The ID of the button to wire up to this instance of TextBox
		/// </summary>
		public string Button
		{
			get{ return _button;}
			set {_button = value;}
		}
		#endregion

		#region PreRender
		/// <summary>
		/// Override OnPreRender and test to see if a Button is wired up to the TextBox. If so, render the javascript
		/// and set the textbox's attributes
		/// </summary>
		protected override void OnPreRender(EventArgs e)
		{
			if(DefaultButton != null)
			{
				ButtonScript();
				this.Attributes.Add("onkeydown", string.Format(OnKeyDownButtonAttribute,DefaultButton.UniqueID));
			}
			base.OnPreRender (e);
		}
		#endregion

		#region JavaScript

		/// <summary>
		/// OnKeyDown Attribute. We use Button, Event ...since just button did not work in FireFox :)
		/// </summary>
		const string OnKeyDownButtonAttribute = "KeyDownHandler('{0}',event);";

		/// <summary>
		/// Renders the javascript function to fire our postback
		/// </summary>
		protected void ButtonScript()
		{
			StringBuilder sb = new StringBuilder();
			sb.Append("<script language=\"javascript\">\n");
			sb.Append("<!--\n");
			sb.Append("function KeyDownHandler(btn,event)\n");
			sb.Append("{\n");
			sb.Append("	if (event.keyCode == 13)\n");
			sb.Append("	{\n");
			sb.Append("		event.returnValue = false;\n");
			sb.Append("		event.cancel = true;\n");
			sb.Append("     __doPostBack(btn)\n");
			sb.Append("	}\n");
			sb.Append("}\n");
			sb.Append("\n");
			sb.Append("//-->\n");
			sb.Append("</script>\n");
			Page.RegisterStartupScript("DefaultButtonScript", sb.ToString());
			Page.RegisterRequiresPostBack(this);
			Page.GetPostBackEventReference(this);
		}
		#endregion
	}
}

⌨️ 快捷键说明

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