using System; using System.Web; using System.Web.UI; using System.Collections.Specialized;
namespace essay { public class MyTextBox: Control, IPostBackDataHandler { //定义控件属性Text public String Text { get {return (String) ViewState["Text"];} set {ViewState["Text"] = value;} } //生成控件对应的HTML代码 protected override void Render(HtmlTextWriter output) { writer.Write("<INPUT TYPE=text name=" + this.UniqueID + " Value='"+this.Text+"' />"); } //定义TextChanged事件委托 public event EventHandler TextChanged; //更新控件的Text状态并返回更新标识 //参数NameValueCollection为回发数据集 public virtual bool LoadPostData(string postDataKey, NameValueCollection values) { String presentValue = Text; String postedValue = values[postDataKey]; if (!presentValue.Equals(postedValue)) { Text = postedValue; return true; } return false; } //检查更新标识引发自定义事件TextChanged public virtual void RaisePostDataChangedEvent() {OnTextChanged(EventArgs.Empty);} //实现回调 protected virtual void OnTextChanged(EventArgs e) {if (TextChanged != null)TextChanged(this,e);} } } |