服務(wù)端事件緣起C# TextBox失去焦點(diǎn)
服務(wù)端事件產(chǎn)生的原因有很多,而其中最不為人知的恐怕就要數(shù)c# textbox失去焦點(diǎn)所引發(fā)的服務(wù)端事件了。也許您會(huì)覺(jué)得有些難以置信,還是讓我用下面的例子來(lái)給您做一個(gè)介紹吧。
在Web應(yīng)用當(dāng)中,我們往往會(huì)用到很多c# textbox失去焦點(diǎn)來(lái)處理錄入的信息。
在頁(yè)面提交之前,在c# textbox失去焦點(diǎn)的時(shí)候,可能就是要處理一下我們輸入的信息。
比如:
1、對(duì)輸入信息的校驗(yàn)
2、根據(jù)輸入的信息對(duì)后面即將錄入的信息的不同處理
3、需要回到服務(wù)端處理
等等...
基于這些要求?。≡赾# textbox失去焦點(diǎn)時(shí)為其加上OnBlur 的服務(wù)端事件就可以了!
服務(wù)端就會(huì)自動(dòng)生成根onclick一樣事件
- this.MyTextBox.OnBlur += new System.EventHandler(this.MyTextBox_OnBlur);
這個(gè)控件主要的地方就是,繼承TextBox,和IPostBackEventHandler接口!公開(kāi)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);
- }
- }
- }
- }
【編輯推薦】