ASP.NET控件開(kāi)發(fā)基礎(chǔ)之事件處理淺析
本次來(lái)介紹控件的事件處理. 我們知道Button控件有OnClick事件,DropDownList控件有SelectedIndexChanged事件.
ASP.NET控件開(kāi)發(fā)基礎(chǔ)之事件處理一.回發(fā)事件和客戶端回發(fā)
下面來(lái)看一個(gè)最簡(jiǎn)單的例子
按鈕單擊事件
- protected void Button1_Click(object sender, EventArgs e)
- {
- Label1.Text = "你好: "+TextBox1.Text;
- }
大家知道Web 服務(wù)器控件創(chuàng)建的按鈕的類型有三種
1.Button
2.LinkButton
3.ImageButton
打開(kāi)MSDN看到三個(gè)控件都繼承IPostBackEventHandler接口
IPostBackEventHandler接口專門定義了處理回發(fā)事件的方法,說(shuō)白了就是onclick事件,如果自定義控件需要處理回發(fā)事件,你就需要繼承IPostBackEventHandler接口,然后實(shí)現(xiàn)接口的RaisePostBackEvent 方法,另外一個(gè)簡(jiǎn)單的方法就是直接繼承Button控件就可以了.
RaisePostBackEvent方法用于處理窗體發(fā)送給服務(wù)器時(shí)引發(fā)的事件,方法中有一個(gè)參數(shù)eventArgument 表示要傳遞到事件處理程序的可選事件參數(shù)的 String
下面總結(jié)處理回發(fā)事件,必須要做的步驟
(1)繼承并實(shí)現(xiàn)IPostBackEventHandler接口的RaisePostBackEvent方法
(2)為表單元素定義UniqueID,以與IPostBackEventHandler服務(wù)器控件的UniqueID相對(duì)應(yīng)
相應(yīng)實(shí)現(xiàn)代碼如下
示例一
- namespace CustomControls
- {
- public class SuperButton1 : Control, IPostBackEventHandler
- {
- // 聲明Click事件委托
- public event EventHandler Click;
- // 定義OnClick事件處理程序
- protected virtual void OnClick(EventArgs e)
- {
- if (Click != null)
- {
- Click(this, e);
- }
- }
- // 實(shí)現(xiàn)RaisePostBackEvent方法,處理回發(fā)事件
- public void RaisePostBackEvent(string eventArgument)
- {
- OnClick(EventArgs.Empty);
- }
- protected override void Render(HtmlTextWriter output)
- {
- output.Write("﹤INPUT TYPE=submit name=" + this.UniqueID +
- " Value='確定' /﹥");
- }
- }
- }
如果你不熟悉委托的話,可以參考一篇叫一個(gè)C#睡前故事的文章
EventArgs.Empty表示沒(méi)有事件數(shù)據(jù)的事件,不要跟我以前一樣認(rèn)為是一個(gè)空的事件,當(dāng)時(shí)就很郁悶,干什么要觸發(fā)空事件呢,都是因?yàn)闆](méi)看清楚Empty字段的意思,以為就為空的意思了.
EventArgs.Empty等同于EventArgs類的構(gòu)造函數(shù),等同于new EventArgs()
注意還在呈現(xiàn)控件的name屬性加了UniqueID.
好了,現(xiàn)在你可以測(cè)試下了.
- protected void SuperButton1_1_Click(object sender, EventArgs e)
- {
- Label1.Text = "你點(diǎn)擊了此按鈕";
- }
這樣你就成功定義了一個(gè)處理回發(fā)事件的控件. 假設(shè)你在頁(yè)面上多次使用這個(gè)控件,編譯器將為每個(gè)事件委托實(shí)例生成一個(gè)字段。如果事件的數(shù)目很大,則一個(gè)委托一個(gè)字段的存儲(chǔ)成本可能無(wú)法接受。.所以推薦采用另外一種優(yōu)化的事件實(shí)現(xiàn)
EventHandlerList 類提供一個(gè)簡(jiǎn)單的委托列表來(lái)添加和刪除委托,下面來(lái)看看更改后的代碼,
AddHandler有兩個(gè)參數(shù)事件對(duì)象和添加的委托,在OnClick事件中必須顯示將委托轉(zhuǎn)換為EventHandler類型
示例二
- using System;
- using System.Web.UI;
- namespace CustomComponents
- {
- public class SuperButton2 : Control, IPostBackEventHandler
- {
- // 聲明Click事件委托
- private static readonly object ClickKey = new object();
- public event EventHandler Click
- {
- add
- {
- Events.AddHandler(ClickKey, value);
- }
- remove
- {
- Events.RemoveHandler(ClickKey, value);
- }
- }
- // 定義OnClick事件處理程序
- protected virtual void OnClick(EventArgs e)
- {
- EventHandler clickEventDelegate =
- (EventHandler)Events[ClickKey];
- if (clickEventDelegate != null)
- {
- clickEventDelegate(this, e);
- }
- }
- // 實(shí)現(xiàn)RaisePostBackEvent方法,處理回發(fā)事件
- public void RaisePostBackEvent(string eventArgument)
- {
- OnClick(new EventArgs());
- }
- protected override void Render(HtmlTextWriter output)
- {
- output.Write("﹤INPUT TYPE=submit name=" + this.UniqueID +
- " Value='確定' /﹥");
- }
- }
- }
下面再來(lái)說(shuō)下客戶端回發(fā)事件,在HTML窗體元素中只有Button按鈕和ImageButton才可以引起窗體回發(fā).
但如LinkButton鏈接按鈕控件要希望啟動(dòng)回發(fā)的話,則要依賴客戶端腳本的事件機(jī)制來(lái)實(shí)現(xiàn)其功能.
在asp.net2.0中,button控件多了一個(gè)UseSubmitBehavior 屬性,指示 Button 控件使用客戶端瀏覽器的提交機(jī)制(客戶端回發(fā))還是 ASP.NET 回發(fā)機(jī)制,默認(rèn)采用回發(fā)機(jī)制,如果設(shè)置為false的話,則需要調(diào)用GetPostBackEventReference 方法來(lái)返回 Button 的客戶端回發(fā)事件
當(dāng)設(shè)置UseSubmitBehavior 屬性為flase時(shí),你運(yùn)行頁(yè)面時(shí),則會(huì)發(fā)現(xiàn)一段自動(dòng)生成的javascript代碼
LinkButton也一樣,再看下面例子,定義了枚舉,定義button按鈕和鏈接按鈕,大家在測(cè)試的時(shí)候,打開(kāi)源代碼就會(huì)發(fā)現(xiàn)不同效果
示例三
- using System;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.ComponentModel;
- namespace CustomComponents
- {
- public enum ButtonDisplay
- {
- Button = 0,
- Hyperlink = 1
- }
- [ToolboxData("﹤{0}:SuperButton3 runat=server﹥﹤/{0}:SuperButton3﹥")]
- public class SuperButton3 : Control, IPostBackEventHandler
- {
- public virtual ButtonDisplay Display
- {
- get
- {
- object display = ViewState["Display"];
- if (display == null)
- return ButtonDisplay.Button;
- else
- return (ButtonDisplay)display;
- }
- set
- {
- ViewState["Display"] = value;
- }
- }
- public virtual string Text
- {
- get
- {
- object text = ViewState["Text"];
- if (text == null)
- return string.Empty;
- else
- return (string)text;
- }
- set
- {
- ViewState["Text"] = value;
- }
- }
- private static readonly object ClickKey = new object();
- public event EventHandler Click
- {
- add
- {
- Events.AddHandler(ClickKey, value);
- }
- remove
- {
- Events.RemoveHandler(ClickKey, value);
- }
- }
- protected virtual void OnClick(EventArgs e)
- {
- EventHandler clickEventDelegate =
- (EventHandler)Events[ClickKey];
- if (clickEventDelegate != null)
- {
- clickEventDelegate(this, e);
- }
- }
- public void RaisePostBackEvent(string argument)
- {
- OnClick(EventArgs.Empty);
- }
- override protected void Render(HtmlTextWriter writer)
- {
- base.Render(writer);
- Page.VerifyRenderingInServerForm(this);
- if (Display == ButtonDisplay.Button)
- {
- writer.Write("﹤INPUT type=\"submit\"");
- writer.Write(" name=\"" + this.UniqueID + "\"");
- writer.Write(" id=\"" + this.UniqueID + "\"");
- writer.Write(" value=\"" + Text + "\"");
- writer.Write(" /﹥");
- }
- else if (Display == ButtonDisplay.Hyperlink)
- {
- writer.Write("﹤A href=\"");
- writer.Write(Page.GetPostBackClientHyperlink(this, ""));
- writer.Write("\"﹥" + Text + "﹤/A﹥");
- }
- }
- }
- }
如果大家本來(lái)就學(xué)過(guò)這方面的知識(shí),看了心里還有譜,如果沒(méi)有的話,里面有些方法不熟悉的話,還是要多看看MSDN. 說(shuō)通俗點(diǎn),回發(fā)事件可以就理解為按鈕單擊事件,而按鈕又分兩種不同的回發(fā)事件方法,這樣講的話,更容易讓人接受,而上面所講的就是實(shí)現(xiàn)按鈕單擊事件實(shí)現(xiàn)的方法.
ASP.NET控件開(kāi)發(fā)基礎(chǔ)之事件處理二.數(shù)據(jù)回發(fā)事件
好了,接著再講數(shù)據(jù)回發(fā).跟上面講的事件回發(fā)有點(diǎn)不同.
下面也舉一個(gè)簡(jiǎn)單的例子,看下圖,有兩個(gè)DropDownList,一個(gè)開(kāi)啟AutoPostBack,一個(gè)沒(méi)有開(kāi)啟,再接著看下面簡(jiǎn)單的代碼,***個(gè)DropDownList,改變下拉框值時(shí),label沒(méi)顯示,按確定按鈕后則顯示label,第二個(gè)DropDownList改變下拉框值時(shí)就顯示了label,因?yàn)殚_(kāi)啟了AutoPostBack.這個(gè)大家都明白吧.
- protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
- {
- Label2.Text = "你選擇了: " + DropDownList1.SelectedItem.Text;
- }
- protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
- {
- Label1.Text = "你選擇了: " + DropDownList2.SelectedItem.Text;
- }
以上實(shí)現(xiàn)的原理就是在SelectedIndexChanged事件里,判斷舊值和新值的比較(比較數(shù)據(jù)),如果發(fā)生變化,則引發(fā)事件,數(shù)據(jù)回發(fā)就是實(shí)現(xiàn)這樣的事件.再重新整理一下思路,明白何時(shí)會(huì)引發(fā)SelectedIndexChanged事件
在選擇下拉框值時(shí),如果選的值跟原來(lái)的值相同,則不觸發(fā)事件,如果選的值跟原來(lái)的值不同的話則觸發(fā)SelectedIndexChanged事件(還是舊值和新值的比較).
打開(kāi)MSDN文檔查看DropDownList 類,則發(fā)現(xiàn)其繼承了 IPostBackDataHandler 接口,我的意思就是說(shuō)想要實(shí)現(xiàn)Change這樣的事件,就要繼承其接口.看看MSDN對(duì)此接口的定義
IPostBackDataHandler 接口
定義 ASP.NET 服務(wù)器控件為自動(dòng)加載回發(fā)數(shù)據(jù)而必須實(shí)現(xiàn)的方法。
LoadPostData 方法 根據(jù)服務(wù)器控件的狀態(tài)由于回發(fā)而發(fā)生更改做出判斷是否調(diào)用RaisePostDataChangedEvent 方法,返回true則調(diào)用(就是舊值和新值不同的時(shí)候)
RaisePostDataChangedEvent 方法用于引發(fā)任何更改事件
以下的例子實(shí)現(xiàn)了如同textbox的TextChanged事件
postDataKey表示控件內(nèi)部數(shù)據(jù)的關(guān)鍵值,postCollection表示所有傳入名稱值的集合,其采用索引的方式來(lái)訪問(wèn)
- using System;
- using System.Web;
- using System.Web.UI;
- using System.Collections.Specialized;
- using System.ComponentModel;
- namespace CustomComponents
- {
- [ToolboxData("﹤{0}:Textbox1 runat=server﹥﹤/{0}:Textbox1﹥"),
- DefaultProperty("Text")]
- public class Textbox1 : Control, IPostBackDataHandler
- {
- public string Text
- {
- get
- {
- object text = ViewState["Text"];
- if (text == null)
- return string.Empty;
- else
- return (string)text;
- }
- set
- {
- ViewState["Text"] = value;
- }
- }
- public bool LoadPostData(string postDataKey,
- NameValueCollection postCollection)
- {
- string postedValue = postCollection[postDataKey];
- //檢查新舊數(shù)據(jù)
- if (!Text.Equals(postedValue))
- {
- Text = postedValue;
- return true;
- //自動(dòng)調(diào)用RaisePostDataChangedEvent()
- }
- else
- return false;
- //不發(fā)生變化
- }
- public void RaisePostDataChangedEvent()
- {
- OnTextChanged(EventArgs.Empty);
- }
- protected virtual void OnTextChanged(EventArgs e)
- {
- if (TextChanged != null)
- TextChanged(this, e);
- }
- public event EventHandler TextChanged;
- override protected void Render(HtmlTextWriter writer)
- {
- base.Render(writer);
- Page.VerifyRenderingInServerForm(this);
- writer.Write("﹤INPUT type=\"text\" name=\"");
- writer.Write(this.UniqueID);
- writer.Write("\" value=\"" + this.Text + "\" /﹥");
- }
- }
- }
上面實(shí)現(xiàn)的方法如同button的onclick事件,其實(shí)不然,而是通過(guò)回發(fā)數(shù)據(jù)的新舊數(shù)據(jù)進(jìn)行判斷,我在示例代碼中加了另外一個(gè)例子,這里就不列出了,大家可以下載后再去看,看了就明白不是button的onclick事件了.
本次主要講了三個(gè)基礎(chǔ)的事件處理
(1)捕獲回發(fā)事件
(2)用于回調(diào)的客戶端腳本
(3)處理回發(fā)數(shù)據(jù)
以下兩個(gè)接口需要你慢慢的熟悉和使用
IPostBackEventHandler接口和IPostBackDataHandler 接口.
想到Button按鈕就要想到IPostBackEventHandler接口,想要textbox,dropdownlist一些change事件則要想要IPostBackDataHandler 接口,如果結(jié)合起來(lái),再自己思考的話,會(huì)明白的更深刻.
可能很多地方我也沒(méi)表達(dá)清楚,跟別人講的很多重復(fù)了,但還要拿出來(lái)分享下,這樣也可以提高自己.***還望大家如果看到有什么錯(cuò)誤,請(qǐng)指出.
ASP.NET控件開(kāi)發(fā)基礎(chǔ)之事件處理的講解就到這里,希望對(duì)你了解ASP.NET控件開(kāi)發(fā)基礎(chǔ)中的事件處理有所幫助。
【編輯推薦】