textboxplus_comp.cs

来自「Microsoft?ASP.NET Programming with Micro」· CS 代码 · 共 102 行

CS
102
字号
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace Chapter_10_Controls
{
	/// <summary>
	/// Summary description for TextBoxPlus_Comp.
	/// </summary>
	[DefaultProperty("Text"), 
	ToolboxData("<{0}:TextBoxPlus_Comp runat=server></{0}:TextBoxPlus_Comp>")]
	public class TextBoxPlus_Comp : System.Web.UI.WebControls.WebControl
	{
		private TextTypes textType;
		private Label label;
		private TextBox textBox;

		[Bindable(true), Category("Appearance"), DefaultValue("Label")]
		public string LabelText
		{
			get
			{
				EnsureChildControls();
				return label.Text;
			}

			set
			{
				EnsureChildControls();
				label.Text = value;
			}
		}

		[Bindable(true), Category("Appearance"), DefaultValue("PlainText")]
		public TextTypes TextType
		{
			get
			{
				EnsureChildControls();
				return textType;
			}

			set
			{
				EnsureChildControls();
				textType = value;
			}
		}

		[Bindable(true), Category("Appearance"), DefaultValue(" ")]
		public string Text
		{
			get
			{
				EnsureChildControls();
				return textBox.Text;
			}

			set
			{
				EnsureChildControls();
				textBox.Text = value;
			}
		}

		protected override void CreateChildControls()
		{
			label = new Label();
			this.Controls.Add(label);

			textBox = new TextBox();
			this.Controls.Add(textBox);
		}

		protected override void OnPreRender(System.EventArgs e)
		{
			if ( Page.IsPostBack )
			{
				if ( textType != TextTypes.PlainText )
				{
					FormatText();
				}
			}
		}

		protected void FormatText()
		{
			EnsureChildControls();
			switch(textType)
			{
				case TextTypes.CurrencyText:
					this.Text = (double.Parse(this.Text)).ToString("C");
					break;
				case TextTypes.DecimalText:
					this.Text = (Convert.ToInt32(this.Text)).ToString("F");
					break;
			}
		}
	}
}

⌨️ 快捷键说明

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