自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

ASP.NET 2.0數(shù)據(jù)綁定控件的自定義集合

開(kāi)發(fā) 后端
本文介紹ASP.NET 2.0數(shù)據(jù)綁定控件的自定義集合。ListControl 是一個(gè)過(guò)于專用的類,它以不受您控制的固定方式執(zhí)行數(shù)據(jù)綁定。

ASP.NET 2.0數(shù)據(jù)綁定控件:管理自定義集合

ListControl 是一個(gè)過(guò)于專用的類,它以不受您控制的固定方式執(zhí)行數(shù)據(jù)綁定 — 除非您重寫諸如 PerformSelect、OnDataBinding 和 PerformDataBinding 之類的方法。它還提供了預(yù)定義的 Items 集合屬性。讓我們?cè)?ASP.NET 2.0 中的更低級(jí)別處理數(shù)據(jù)綁定,并且設(shè)計(jì)具有下列功能的 ButtonList 控件:

◆使用自定義集合類來(lái)保留組成項(xiàng)

◆用自定義方式管理視圖狀態(tài)

ButtonList 控件是另一個(gè)為每個(gè)綁定數(shù)據(jù)項(xiàng)輸出按鈕的列表控件。您可以讓它從 ListControl 繼承;而且,您可以獲得 HeadlineList 的源代碼,將 Label 替換為 Button,而它仍然應(yīng)當(dāng)正常工作。這一次,我將采用一種不同的方法來(lái)說(shuō)明 DataBoundControl 的行為。為簡(jiǎn)單起見(jiàn),我仍將跳過(guò) IRepeatInfoUser 接口。

  1. public class ButtonList : System.Web.UI.WebControls.DataBoundControl  
  2. {  
  3.    :  
  4. }  

標(biāo)題和命令名稱表現(xiàn)了每個(gè)按鈕的性質(zhì)。該信息是通過(guò)幾個(gè)自定義屬性(如 DataTextField 和 DataCommandField)從綁定數(shù)據(jù)源中獲得的。您可以容易地添加類似的屬性,以提供數(shù)據(jù)綁定工具提示,甚至提供 URL。

  1. public virtual string DataCommandField  
  2. {  
  3.    get 
  4.    {  
  5.       object o = ViewState["DataCommandField"];  
  6.       if (o == null)  
  7.          return "";  
  8.       return (string)o;  
  9.    }  
  10.    set { ViewState["DataCommandField"] = value; }  
  11. }  

所發(fā)現(xiàn)的有關(guān)每個(gè)綁定按鈕的所有信息都被填充到一個(gè)通過(guò) Items 屬性公開(kāi)的自定義對(duì)象集合中。(請(qǐng)注意,Items 只是該屬性的標(biāo)準(zhǔn)、慣用而任意的名稱。)

  1. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]  
  2. [PersistenceMode(PersistenceMode.InnerProperty)]  
  3. public virtual ButtonItemCollection Items  
  4. {  
  5.    get 
  6.    {  
  7.       if (_items == null)  
  8.       {  
  9.          _items = new ButtonItemCollection();  
  10.          if (base.IsTrackingViewState)  
  11.             _items.TrackViewState();  
  12.       }  
  13.       return _items;  
  14.    }  
  15. }  

Items 集合是自定義 ButtonItemCollection 類的實(shí)例 — ButtonItem 對(duì)象的集合。ButtonItem 類只是存儲(chǔ)了有關(guān)綁定按鈕的關(guān)鍵信息 — Text 和 CommandName 屬性,外加幾個(gè)構(gòu)造函數(shù)以及 ToString 方法。ButtonItem 類是作為普通列表控件的 ListItem 類的對(duì)等物。下面是一個(gè)示例。

  1. public class ButtonItem  
  2. {  
  3.    private string _text;  
  4.    private string _command;  
  5.  
  6.    public ButtonItem(string text, string command) {  
  7.       _text = text;  
  8.       _command = command;  
  9.    }  
  10.    public string Text {  
  11.       get {return _text;}  
  12.       set {_text = value;}  
  13.    }  
  14.    public string CommandName {  
  15.       get { return _command; }  
  16.       set { _command = value; }  
  17.    }  
  18.    public override string ToString() {  
  19.       return "Button [" + Text + "]";  
  20.    }  
  21. }  
  22.  

現(xiàn)在,如何創(chuàng)建 ButtonItem 對(duì)象的集合呢?在 ASP.NET 1.x 中,您必須生成一個(gè)從 CollectionBase 繼承的自定義集合類,并且起碼重寫幾個(gè)方法。然而,自定義集合只是圍繞 ArrayList 對(duì)象的包裝而已,在訪問(wèn)速度方面并沒(méi)有任何真正的優(yōu)勢(shì)。實(shí)際上,仍然需要進(jìn)行轉(zhuǎn)換。.NET 2.0 中的泛型提供了真正的轉(zhuǎn)折點(diǎn)。要生成 ButtonItem 對(duì)象集合,您需要以下代碼:

  1. public class ButtonItemCollection : Collection < ButtonItem>  
  2. {  
  3. }  

并且,它的性能也會(huì)更好,因?yàn)榫幾g器在幕后完成了某些工作。ButtonList 控件只需要兩個(gè)被重寫的方法:Render 和 PerformDataBinding。Render 假定 Items 集合被填充;因此,它只是進(jìn)行迭代并輸出標(biāo)記代碼。

  1. protected override void Render(HtmlTextWriter writer)  
  2. {  
  3.    for(int i=0; i< } btn.RenderControl(writer); btn.CommandName="item.CommandName;" btn.Text="item.Text;" Button(); btn="new" Button item="Items[i];" ButtonItem { i++)>  

ASP.NET 2.0數(shù)據(jù)綁定控件:Items集合的重要性

Items 集合為什么如此重要?它可以幫助您獲得兩個(gè)結(jié)果。首先,您可以用手動(dòng)添加的項(xiàng)填充該列表控件。其次,一旦在視圖狀態(tài)中持久保存該集合,您就可以在回發(fā)時(shí)重新生成該控件的用戶界面,而無(wú)須綁定到數(shù)據(jù)。在進(jìn)行數(shù)據(jù)綁定時(shí),Items 集合是在何處以及由誰(shuí)填充的呢?這需要用到 PerformDataBinding。該方法獲得一個(gè)可枚舉的數(shù)據(jù)列表(無(wú)論原始數(shù)據(jù)源是什么)并使用它來(lái)填充 Items 集合。

  1. protected override void PerformDataBinding(IEnumerable dataSource)  
  2. {  
  3.    base.PerformDataBinding(dataSource);  
  4.    string textField = DataTextField;  
  5.    string commandField = DataCommandField;  
  6.  
  7.    if (dataSource != null) {  
  8.    foreach (object o in dataSource)  
  9.    {  
  10.       ButtonItem item = new ButtonItem();  
  11.       item.Text = DataBinder.GetPropertyValue(o, textField, null);  
  12.       item.CommandName = DataBinder.GetPropertyValue(o,   
  13.                                              DataCommandField, null);  
  14.       Items.Add(item);  
  15.    }   
  16.    }  
  17. }  
  18.  

每當(dāng)需要進(jìn)行數(shù)據(jù)綁定時(shí),該方法都能夠確保 Items 集合被填充。在回發(fā)時(shí)會(huì)發(fā)生什么?在這種情況下,必須根據(jù)視圖狀態(tài)重新構(gòu)建 Items 集合。您可以通過(guò) IStateManager 接口上的方法賦予自定義集合類這一能力。以下為該接口的關(guān)鍵方法:

  1. public void LoadViewState(object state)  
  2. {  
  3.    if (state != null) {  
  4.       Pair p = (Pair) state;  
  5.       Clear();  
  6.       string[] rgText = (string[])p.First;  
  7.       string[] rgCommand = (string[])p.Second;  
  8.  
  9.       for (int i = 0; i < rgText.Length; i++)  
  10.          Add(new ButtonItem(rgText[i], rgCommand[i]));  
  11.    }  
  12. }  
  13.  
  14. public object SaveViewState()  
  15. {  
  16.    int numOfItems = Count;  
  17.    object[] rgText = new string[numOfItems];  
  18.    object[] rgCommand = new string[numOfItems];  
  19.  
  20.    for (int i = 0; i < numOfItems; i++) {  
  21.       rgText[i] = this[i].Text;  
  22.       rgCommand[i] = this[i].CommandName;  
  23.    }  
  24.  
  25.    return new Pair(rgText, rgCommand);  
  26. }  
  27.  

該類使用一個(gè) Pair 對(duì)象(一種經(jīng)過(guò)優(yōu)化的 2 位置數(shù)組)將自身序列化為視圖狀態(tài)。您需要?jiǎng)?chuàng)建兩個(gè)對(duì)象數(shù)組,以便保留每個(gè)按鈕的文本和命令名稱。這兩個(gè)數(shù)組隨后被成對(duì)打包并插入到該視圖狀態(tài)中。當(dāng)還原該視圖狀態(tài)時(shí),會(huì)將該數(shù)組對(duì)拆包,并且使用先前存儲(chǔ)的信息重新填充 Items 集合。使用該方法要比使 ButtonItem 類可序列化更可取,因?yàn)閭鹘y(tǒng)的二進(jìn)制格式化程序的性能(在空間和時(shí)間這兩個(gè)方面)更差。

然而,向集合中添加視圖狀態(tài)支持還不夠。還必須增強(qiáng) ButtonList 控件以利用集合的序列化功能。您可以重寫控件類上的 LoadViewState 和 SaveViewState。

  1. protected override void LoadViewState(object savedState)  
  2. {  
  3.    if (savedState != null) {  
  4.       Pair p = (Pair) savedState;  
  5.       base.LoadViewState(p.First);  
  6.       Items.LoadViewState(p.Second);  
  7.    }  
  8.    else 
  9.       base.LoadViewState(null);  
  10. }  
  11.  
  12. protected override object SaveViewState()  
  13. {  
  14.    object baseState = base.SaveViewState();  
  15.    object itemState = Items.SaveViewState();  
  16.    if ((baseState == null) && (itemState == null))  
  17.       return null;  
  18.    return new Pair(baseState, itemState);  
  19. }  
  20.  

控件的視圖狀態(tài)由兩個(gè)元素組成:默認(rèn)控件的視圖狀態(tài)以及 Items 集合。這兩個(gè)對(duì)象被打包到 Pair 對(duì)象中。除了 Pair 對(duì)象以外,您還可以使用 Triplet 對(duì)象(包含三個(gè)對(duì)象的數(shù)組),或者使用 Pair 或 Triplet 對(duì)組成任意數(shù)量的對(duì)象。

以這種方式設(shè)計(jì)的自定義集合還可以在設(shè)計(jì)時(shí)滿足需要。Visual Studio 2005 中嵌入的默認(rèn)集合編輯器可以識(shí)別該集合并彈出如圖 3 所示的對(duì)話框。

設(shè)計(jì)時(shí)的 ButtonList Items 集合 

ASP.NET 2.0數(shù)據(jù)綁定控件:設(shè)計(jì)時(shí)的 ButtonList Items 集合

值得說(shuō)明的是,在 ASP.NET 2.0 中,某些數(shù)據(jù)綁定控件使您可以將數(shù)據(jù)綁定項(xiàng)與以編程方式通過(guò) Items 集合添加的項(xiàng)分開(kāi)。布爾型的 AppendDataBoundItems 屬性用于控制該控件的編程接口的這一方面。該屬性在 ListControl(而非 DataBoundControl)上定義,并且默認(rèn)為 false。

【編輯推薦】

  1. 列表控件示例:HeadlineList
  2. ASP.NET 2.0數(shù)據(jù)綁定機(jī)制:生成控件
  3. ASP.NET 2.0數(shù)據(jù)綁定的發(fā)展簡(jiǎn)述
  4. 概述ASP.NET調(diào)用Excel進(jìn)程
  5. ASP.NET開(kāi)發(fā)技巧之Theme功能淺析
責(zé)任編輯:yangsai 來(lái)源: MSDN
相關(guān)推薦

2009-08-05 17:43:48

ASP.NET 2.0

2009-08-06 17:13:56

ASP.NET自定義控

2009-07-28 09:32:41

ASP.NET自定義控

2009-08-10 14:16:59

ASP.NET自定義控

2009-07-31 10:23:09

ASP.NET源碼DateTimePic

2009-07-22 17:21:27

ASP.NET 2.0

2009-07-28 14:06:28

ASP.NET 2.0

2011-04-19 10:33:16

ASP.NET自定義控

2009-08-06 09:18:01

ASP.NET自定義控ASP.NET控件開(kāi)發(fā)

2009-08-05 17:26:25

ASP.NET 2.0

2009-08-07 15:34:15

ASP.NET數(shù)據(jù)綁定

2009-07-24 17:15:52

SiteMapData

2011-05-19 10:16:27

ASP.NET

2009-08-06 17:52:45

ASP.NET控件開(kāi)發(fā)自定義控件

2009-08-01 12:00:15

ASP.NET服務(wù)器自ASP.NET服務(wù)器ASP.NET

2009-07-27 09:01:44

ObjectDataS

2009-07-21 15:27:12

ASP.NET 2.0

2009-08-03 18:15:05

ASP.NET數(shù)據(jù)綁定

2009-08-07 15:45:26

ASP.NET復(fù)合控件數(shù)據(jù)綁定

2009-08-04 13:35:16

ASP.NET自定義樣
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)