成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

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

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

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

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

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

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

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

標(biāo)題和命令名稱表現(xiàn)了每個按鈕的性質(zhì)。該信息是通過幾個自定義屬性(如 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)每個綁定按鈕的所有信息都被填充到一個通過 Items 屬性公開的自定義對象集合中。(請注意,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 對象的集合。ButtonItem 類只是存儲了有關(guān)綁定按鈕的關(guān)鍵信息 — Text 和 CommandName 屬性,外加幾個構(gòu)造函數(shù)以及 ToString 方法。ButtonItem 類是作為普通列表控件的 ListItem 類的對等物。下面是一個示例。

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

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

并且,它的性能也會更好,因?yàn)榫幾g器在幕后完成了某些工作。ButtonList 控件只需要兩個被重寫的方法: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 集合為什么如此重要?它可以幫助您獲得兩個結(jié)果。首先,您可以用手動添加的項(xiàng)填充該列表控件。其次,一旦在視圖狀態(tài)中持久保存該集合,您就可以在回發(fā)時重新生成該控件的用戶界面,而無須綁定到數(shù)據(jù)。在進(jìn)行數(shù)據(jù)綁定時,Items 集合是在何處以及由誰填充的呢?這需要用到 PerformDataBinding。該方法獲得一個可枚舉的數(shù)據(jù)列表(無論原始數(shù)據(jù)源是什么)并使用它來填充 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ù)綁定時,該方法都能夠確保 Items 集合被填充。在回發(fā)時會發(fā)生什么?在這種情況下,必須根據(jù)視圖狀態(tài)重新構(gòu)建 Items 集合。您可以通過 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.  

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

然而,向集合中添加視圖狀態(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)由兩個元素組成:默認(rèn)控件的視圖狀態(tài)以及 Items 集合。這兩個對象被打包到 Pair 對象中。除了 Pair 對象以外,您還可以使用 Triplet 對象(包含三個對象的數(shù)組),或者使用 Pair 或 Triplet 對組成任意數(shù)量的對象。

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

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

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

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

【編輯推薦】

  1. 列表控件示例:HeadlineList
  2. ASP.NET 2.0數(shù)據(jù)綁定機(jī)制:生成控件
  3. ASP.NET 2.0數(shù)據(jù)綁定的發(fā)展簡述
  4. 概述ASP.NET調(diào)用Excel進(jìn)程
  5. ASP.NET開發(fā)技巧之Theme功能淺析
責(zé)任編輯:yangsai 來源: 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-28 14:06:28

ASP.NET 2.0

2009-07-22 17:21:27

ASP.NET 2.0

2009-07-31 10:23:09

ASP.NET源碼DateTimePic

2011-04-19 10:33:16

ASP.NET自定義控

2009-08-06 09:18:01

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

2009-08-05 17:26:25

ASP.NET 2.0

2009-07-24 17:15:52

SiteMapData

2009-08-07 15:34:15

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

2009-08-01 12:00:15

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

2011-05-19 10:16:27

ASP.NET

2009-08-06 17:52:45

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

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-07 11:12:58

ASP.NET控件開發(fā)
點(diǎn)贊
收藏

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

主站蜘蛛池模板: 鸳鸯谱在线观看高清 | 国产欧美一区二区三区另类精品 | 成人精品一区二区 | 国产精品视频导航 | 精品亚洲一区二区三区四区五区 | 天天射天天干 | 国产欧美日韩精品一区二区三区 | 欧美国产日韩在线观看 | 特一级毛片 | 精品免费国产一区二区三区 | 国产精品自拍视频 | 国产欧美一区二区三区另类精品 | 成人动漫视频网站 | 日韩精品一区在线观看 | 天天干天天操天天看 | 国产精品色婷婷久久58 | 国产视频一区在线 | 久久久久国产精品午夜一区 | 能看的av| 午夜在线精品 | 欧美高清视频在线观看 | 国产91网址| 看片国产 | 精品国产乱码久久久久久闺蜜 | 国产在线第一页 | 欧美a在线| 在线观看视频一区 | 精品在线一区 | 亚洲美女一区二区三区 | 国产精品视频免费播放 | 午夜激情影院 | 日韩一区二区三区在线播放 | 欧洲精品码一区二区三区免费看 | 午夜视频免费在线观看 | 久久青草av | 亚州精品成人 | 久久亚洲欧美日韩精品专区 | 视频在线观看一区二区 | 国产精品成人一区二区三区吃奶 | 91精品欧美久久久久久久 | 亚洲欧美日韩中文在线 |