在Web應用當中!我們往往會用到很多TextBox來處理錄入的信息。
在頁面提交之前,在TextBox失去焦點的時候,可能就是要處理一下我們輸入的信息。
比如:
1、對輸入信息的校驗
2、根據輸入的信息對后面即將錄入的信息的不同處理
3、需要回到服務端處理
等等...
基于這些要求啊!給TextBox加上OnBlur 的服務端事件就可以了!如圖:
服務端就會自動生成根onclick一樣事件
this.MyTextBox.OnBlur += new System.EventHandler(this.MyTextBox_OnBlur);
這個控件主要的地方就是,繼承TextBox,和IPostBackEventHandler接口!公開OnBlur事件就可以了!
完整的代碼如下:
using System;
namespace Region.Controls {
public class PostBackTextBox : System.Web.UI.WebControls.TextBox,System.Web.UI.IPostBackEventHandler
{ protected override void Render(System.Web.UI.HtmlTextWriter writer) { Attributes["onblur"] = Page.GetPostBackEventReference(this); base.Render (writer); } public event EventHandler OnBlur; public virtual void RaisePostBackEvent(string eventArgument) { if (OnBlur != null) { OnBlur(this, null); } }
} }
|