日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
ASP.NET數(shù)據(jù)綁定控件開發(fā)淺析

本篇將開始介紹如自定義ASP.NET數(shù)據(jù)綁定控件,這里感謝很多人的支持,有你們的支持很高興.

成都創(chuàng)新互聯(lián)公司憑借專業(yè)的設計團隊扎實的技術(shù)支持、優(yōu)質(zhì)高效的服務意識和豐厚的資源優(yōu)勢,提供專業(yè)的網(wǎng)站策劃、成都做網(wǎng)站、成都網(wǎng)站建設、網(wǎng)站優(yōu)化、軟件開發(fā)、網(wǎng)站改版等服務,在成都十載的網(wǎng)站建設設計經(jīng)驗,為成都成百上千中小型企業(yè)策劃設計了網(wǎng)站。

這里首先需要大家熟悉ASP.NET模板控件的使用,還有自定義模板控件.因為數(shù)據(jù)綁定控件多是基于模板控件的.

ASP.NET數(shù)據(jù)綁定控件一.回顧

如果你使用過ASP.NET內(nèi)置的數(shù)據(jù)控件(如DataList,Repeater),你一定會這么做

1.設置數(shù)據(jù)源 DataSource屬性

2.調(diào)用數(shù)據(jù)綁定  DataBind方法

3.在控件的不同模板內(nèi)使用綁定語法顯示數(shù)據(jù)

這三步應該是必須要做的

其他更多的

你可能需要對綁定的數(shù)據(jù)進行統(tǒng)一的一些操作(如時間格式化),或者對數(shù)據(jù)的某一項進行操作(對某一項進行格式化),或者需要觸發(fā)模板控件內(nèi)的一些事件(如databound事件).

根據(jù)上面的一些需求,我們需要這樣做

1.對綁定的數(shù)據(jù)進行統(tǒng)一的一些操作: 為數(shù)據(jù)綁定控件定義Item項(表示列表的一條數(shù)據(jù), 如Repeater的RepeaterItem)

2.對數(shù)據(jù)的某一項進行操作: 因為定義了Item項,那你肯定需要一個ItemCollection集合,其可以方便的為你檢索數(shù)據(jù)

3.因為定義了RepeaterItem,原先的EventArgs和CommandEventArgs已經(jīng)無法滿足需求,我們需要自定義委托及其一個為控件提供數(shù)據(jù)的的ItemEventArgs

上面三點有些并非必須定義,如第2點,還需要根據(jù)具體需求來定.但一個完成的控件是需要的.

ASP.NET數(shù)據(jù)綁定控件二.為數(shù)據(jù)控件做好準備

這次的demo為不完整的Datalist控件,來源還是MSDN的例子,我們命名為TemplatedList,此控件未定義ItemCollection集合

好了,根據(jù)上面的分析我們先為TemplatedList提供項和委托及為事件提供數(shù)據(jù)的幾個EventArgs,請看下面類圖

1.TemplatedListCommandEventArgs為Command事件提供數(shù)據(jù)

2.TemplatedListItemEventArgs為一般項提供數(shù)據(jù)

3.TemplatedListItem表示TemplatedList的項

ASP.NET數(shù)據(jù)綁定控件三.編寫TemplatedList

1.TemplatedList主要功能簡介

提供一個ItemTemplate模板屬性,提供三種不同項樣式,ItemCommand 事件冒泡事件及4個事件

2.實現(xiàn)主要步驟

以下為必須

(1)控件必須實現(xiàn) System.Web.UI.INamingContainer 接口

(2)定義至少一個模板屬性

(3)定義DataSource數(shù)據(jù)源屬性

(4)定義控件項DataItem,即模板的一個容器

(5)重寫DataBind 方法及復合控件相關(guān)方法(模板控件為特殊的復合控件)

當然還有其他額外的屬性,樣式,事件

3.具體實現(xiàn)

下面我們來具體看實現(xiàn)方法

(1)定義控件成員屬性

 
 
 
 
  1. #region 靜態(tài)變量
  2.         private static readonly object EventSelectedIndexChanged = new object();
  3.         private static readonly object EventItemCreated = new object();
  4.         private static readonly object EventItemDataBound = new object();
  5.         private static readonly object EventItemCommand = new object();
  6.         #endregion
  7.         成員變量#region 成員變量
  8.         private IEnumerable dataSource;
  9.         private TableItemStyle itemStyle;
  10.         private TableItemStyle alternatingItemStyle;
  11.         private TableItemStyle selectedItemStyle;
  12.         private ITemplate itemTemplate;
  13.         #endregion
  14.         控件屬性#region 控件屬性
  15.         [
  16.         Category("Style"),
  17.         Description("交替項樣式"),
  18.         DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
  19.         NotifyParentProperty(true),
  20.         PersistenceMode(PersistenceMode.InnerProperty),
  21.         ]
  22.         public virtual TableItemStyle AlternatingItemStyle
  23.         {
  24.             get
  25.             {
  26.                 if (alternatingItemStyle == null)
  27.                 {
  28.                     alternatingItemStyle = new TableItemStyle();
  29.                     if (IsTrackingViewState)
  30.                         ((IStateManager)alternatingItemStyle).TrackViewState();
  31.                 }
  32.                 return alternatingItemStyle;
  33.             }
  34.         }
  35.         [
  36.         Category("Style"),
  37.         Description("一般項樣式"),
  38.         DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
  39.         NotifyParentProperty(true),
  40.         PersistenceMode(PersistenceMode.InnerProperty),
  41.         ]
  42.         public virtual TableItemStyle ItemStyle
  43.         {
  44.             get
  45.             {
  46.                 if (itemStyle == null)
  47.                 {
  48.                     itemStyle = new TableItemStyle();
  49.                     if (IsTrackingViewState)
  50.                         ((IStateManager)itemStyle).TrackViewState();
  51.                 }
  52.                 return itemStyle;
  53.             }
  54.         }
  55.         [
  56.          Category("Style"),
  57.          Description("選中項樣式"),
  58.          DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
  59.          NotifyParentProperty(true),
  60.          PersistenceMode(PersistenceMode.InnerProperty),
  61.          ]
  62.         public virtual TableItemStyle SelectedItemStyle
  63.         {
  64.             get
  65.             {
  66.                 if (selectedItemStyle == null)
  67.                 {
  68.                     selectedItemStyle = new TableItemStyle();
  69.                     if (IsTrackingViewState)
  70.                         ((IStateManager)selectedItemStyle).TrackViewState();
  71.                 }
  72.                 return selectedItemStyle;
  73.             }
  74.         }
  75.         [
  76.         Bindable(true),
  77.         Category("Appearance"),
  78.         DefaultValue(-1),
  79.         Description("The cell padding of the rendered table.")
  80.         ]
  81.         public virtual int CellPadding
  82.         {
  83.             get
  84.             {
  85.                 if (ControlStyleCreated == false)
  86.                 {
  87.                     return -1;
  88.                 }
  89.                 return ((TableStyle)ControlStyle).CellPadding;
  90.             }
  91.             set
  92.             {
  93.                 ((TableStyle)ControlStyle).CellPadding = value;
  94.             }
  95.         }
  96.         [
  97.         Bindable(true),
  98.         Category("Appearance"),
  99.         DefaultValue(0),
  100.         Description("The cell spacing of the rendered table.")
  101.         ]
  102.         public virtual int CellSpacing
  103.         {
  104.             get
  105.             {
  106.                 if (ControlStyleCreated == false)
  107.                 {
  108.                     return 0;
  109.                 }
  110.                 return ((TableStyle)ControlStyle).CellSpacing;
  111.             }
  112.             set
  113.             {
  114.                 ((TableStyle)ControlStyle).CellSpacing = value;
  115.             }
  116.         }
  117.         [
  118.         Bindable(true),
  119.         Category("Appearance"),
  120.         DefaultValue(GridLines.None),
  121.         Description("The grid lines to be shown in the rendered table.")
  122.         ]
  123.         public virtual GridLines GridLines
  124.         {
  125.             get
  126.             {
  127.                 if (ControlStyleCreated == false)
  128.                 {
  129.                     return GridLines.None;
  130.                 }
  131.                 return ((TableStyle)ControlStyle).GridLines;
  132.             }
  133.             set
  134.             {
  135.                 ((TableStyle)ControlStyle).GridLines = value;
  136.             }
  137.         }
  138.         [
  139.         Bindable(true),
  140.         Category("Data"),
  141.         DefaultValue(null),
  142.         Description("數(shù)據(jù)源"),
  143.         DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
  144.         ]
  145.         public IEnumerable DataSource
  146.         {
  147.             get
  148.             {
  149.                 return dataSource;
  150.             }
  151.             set
  152.             {
  153.                 dataSource = value;
  154.             }
  155.         }
  156.         [
  157.         Browsable(false),
  158.         DefaultValue(null),
  159.         Description("項模板"),
  160.         PersistenceMode(PersistenceMode.InnerProperty),
  161.         TemplateContainer(typeof(TemplatedListItem))
  162.         ]
  163.         public virtual ITemplate ItemTemplate
  164.         {
  165.             get
  166.             {
  167.                 return itemTemplate;
  168.             }
  169.             set
  170.             {
  171.                 itemTemplate = value;
  172.             }
  173.         }
  174.         [
  175.         Bindable(true),
  176.         DefaultValue(-1),
  177.         Description("選中項索引,默認為-1")
  178.         ]
  179.         public virtual int SelectedIndex
  180.         {
  181.             get
  182.             {
  183.                 object o = ViewState["SelectedIndex"];
  184.                 if (o != null)
  185.                     return (int)o;
  186.                 return -1;
  187.             }
  188.             set
  189.             {
  190.                 if (value < -1)
  191.                 {
  192.                     throw new ArgumentOutOfRangeException();
  193.                 }
  194.                 //獲取上次選中項
  195.                 int oldSelectedIndex = SelectedIndex;
  196.                 ViewState["SelectedIndex"] = value;
  197.                 if (HasControls())
  198.                 {
  199.                     Table table = (Table)Controls[0];
  200.                     TemplatedListItem item;
  201.                     //第一次選中項不執(zhí)行
  202.                     if ((oldSelectedIndex != -1) && (table.Rows.Count > oldSelectedIndex))
  203.                     {
  204.                         item = (TemplatedListItem)table.Rows[oldSelectedIndex];
  205.                         //判斷項類型,為了將選中項還原為數(shù)據(jù)項
  206.                         if (item.ItemType != ListItemType.EditItem)
  207.                         {
  208.                             ListItemType itemType = ListItemType.Item;
  209.                             if (oldSelectedIndex % 2 != 0)
  210.                                 itemType = ListItemType.AlternatingItem;
  211.                             item.SetItemType(itemType);
  212.                         }
  213.                     }
  214.                     //第一次執(zhí)行此項,并一直執(zhí)行
  215.                     if ((value != -1) && (table.Rows.Count > value))
  216.                     {
  217.                         item = (TemplatedListItem)table.Rows[value];
  218.                         item.SetItemType(ListItemType.SelectedItem);
  219.                     }
  220.                 }
  221.             }
  222.         }
  223.  
  224.         #endregion

成員如下(可以看上面類圖)

1.三個項樣式和三個樣式屬性

2.公開DataSource數(shù)據(jù)源屬性,一個模板屬性

3.SelectedIndex索引屬性

前面的相信大家都很容易明白,其中的三個項樣式我們需要為其重寫視圖狀態(tài)管理,不熟悉可以看以前的隨筆,這里不再重復.

SelectedIndex屬性比較復雜,這里重點介紹此屬性

SelectedIndex索引屬性默認為-1,

我給出了注釋,在賦值前先記錄下了上次的選中項,為恢復樣式而做準備

 
 
 
 
  1. //獲取上次選中項
  2.  int oldSelectedIndex = SelectedIndex;
  3.  ViewState["SelectedIndex"] = value;

當?shù)谝淮胃腟electedIndex屬性時只執(zhí)行下列代碼(將此項標記為選中項),因為初始化時的沒有oldSelectedIndex,不需要恢復樣式

 
 
 
 
  1. //第一次執(zhí)行此項,并一直執(zhí)行
  2.                     if ((value != -1) && (table.Rows.Count > value))
  3.                     {
  4.                         item = (TemplatedListItem)table.Rows[value];
  5.                         item.SetItemType(ListItemType.SelectedItem);
  6.                     }

再次執(zhí)行時,恢復oldSelectedIndex選中項樣式

 
 
 
 
  1. //第一次選中項不執(zhí)行
  2. if ((oldSelectedIndex != -1) && (table.Rows.Count > oldSelectedIndex))
  3. {
  4.     item = (TemplatedListItem)table.Rows[oldSelectedIndex];
  5.     //判斷項類型,為了將選中項還原為數(shù)據(jù)項
  6.     if (item.ItemType != ListItemType.EditItem)
  7.     {
  8.         ListItemType itemType = ListItemType.Item;
  9.         if (oldSelectedIndex % 2 != 0)
  10.             itemType = ListItemType.AlternatingItem;
  11.         item.SetItemType(itemType);
  12.     }
  13. }

相信這樣的解釋你會明白

(2)定義控件成員事件

我們可以用上剛才我們聲明的委托了,即然你定義了這么多事件,就該為其安排觸發(fā)的先后.所以這個要特別注意,等下會再次提到.

 
 
 
 
  1. #region 事件
  2.         protected virtual void OnItemCommand(TemplatedListCommandEventArgs e)
  3.         {
  4.             TemplatedListCommandEventHandler onItemCommandHandler = (TemplatedListCommandEventHandler)Events[EventItemCommand];
  5.             if (onItemCommandHandler != null) onItemCommandHandler(this, e);
  6.         }
  7.         protected virtual void OnItemCreated(TemplatedListItemEventArgs e)
  8.         {
  9.             TemplatedListItemEventHandler onItemCreatedHandler = (TemplatedListItemEventHandler)Events[EventItemCreated];
  10.             if (onItemCreatedHandler != null) onItemCreatedHandler(this, e);
  11.         }
  12.         protected virtual void OnItemDataBound(TemplatedListItemEventArgs e)
  13.         {
  14.             TemplatedListItemEventHandler onItemDataBoundHandler = (TemplatedListItemEventHandler)Events[EventItemDataBound];
  15.             if (onItemDataBoundHandler != null) onItemDataBoundHandler(this, e);
  16.         }
  17.         protected virtual void OnSelectedIndexChanged(EventArgs e)
  18.         {
  19.             EventHandler handler = (EventHandler)Events[EventSelectedIndexChanged];
  20.             if (handler != null) handler(this, e);
  21.         }
  22.         [
  23.         Category("Action"),
  24.         Description("Raised when a CommandEvent occurs within an item.")
  25.         ]
  26.         public event TemplatedListCommandEventHandler ItemCommand
  27.         {
  28.             add
  29.             {
  30.                 Events.AddHandler(EventItemCommand, value);
  31.             }
  32.             remove
  33.             {
  34.                 Events.RemoveHandler(EventItemCommand, value);
  35.             }
  36.         }
  37.         [
  38.         Category("Behavior"),
  39.         Description("Raised when an item is created and is ready for customization.")
  40.         ]
  41.         public event TemplatedListItemEventHandler ItemCreated
  42.         {
  43.             add
  44.             {
  45.                 Events.AddHandler(EventItemCreated, value);
  46.             }
  47.             remove
  48.             {
  49.                 Events.RemoveHandler(EventItemCreated, value);
  50.             }
  51.         }
  52.         [
  53.         Category("Behavior"),
  54.         Description("Raised when an item is data-bound.")
  55.         ]
  56.         public event TemplatedListItemEventHandler ItemDataBound
  57.         {
  58.             add
  59.             {
  60.                 Events.AddHandler(EventItemDataBound, value);
  61.             }
  62.             remove
  63.             {
  64.                 Events.RemoveHandler(EventItemDataBound, value);
  65.             }
  66.         }
  67.         [
  68.         Category("Action"),
  69.         Description("Raised when the SelectedIndex property has changed.")
  70.         ]
  71.         public event EventHandler SelectedIndexChanged
  72.         {
  73.             add
  74.             {
  75.                 Events.AddHandler(EventSelectedIndexChanged, value);
  76.             }
  77.             remove
  78.             {
  79.                 Events.RemoveHandler(EventSelectedIndexChanged, value);
  80.             }
  81.         }
  82.         #endregion

(3)關(guān)鍵實現(xiàn)

我們?yōu)榭丶峁┝诉@么多東西,剩下的事情就是要真正去實現(xiàn)功能了

1.重寫DataBind方法

當控件綁定數(shù)據(jù)時首先會執(zhí)行此方法觸發(fā)DataBinding事件

 
 
 
 
  1. //控件執(zhí)行綁定時執(zhí)行
  2. public override void DataBind()
  3. {
  4.     base.OnDataBinding(EventArgs.Empty);
  5.     //移除控件
  6.     Controls.Clear();
  7.     //清除視圖狀態(tài)信息
  8.     ClearChildViewState();
  9.     //創(chuàng)建一個帶或不帶指定數(shù)據(jù)源的控件層次結(jié)構(gòu)
  10.     CreateControlHierarchy(true);
  11.     ChildControlsCreated = true;
  12.     TrackViewState();
  13. }

2.CreateControlHierarchy方法

 
 
 
 
  1. /**//// 
  2.  /// 創(chuàng)建一個帶或不帶指定數(shù)據(jù)源的控件層次結(jié)構(gòu)
  3.  /// 
  4.  /// 指示是否要使用指定的數(shù)據(jù)源
  5.  //注意:當?shù)诙螆?zhí)行數(shù)據(jù)綁定時,會執(zhí)行兩遍
  6.  private void CreateControlHierarchy(bool useDataSource)
  7.  {
  8.      IEnumerable dataSource = null;
  9.      int count = -1;
  10.      if (useDataSource == false)
  11.      {
  12.          // ViewState must have a non-null value for ItemCount because this is checked 
  13.          //  by CreateChildControls.
  14.          count = (int)ViewState["ItemCount"];
  15.          if (count != -1)
  16.          {
  17.              dataSource = new DummyDataSource(count);
  18.          }
  19.      }
  20.      else
  21.      {
  22.          dataSource = this.dataSource;
  23.      }
  24.      //根據(jù)項類型開始創(chuàng)建子控件
  25.      if (dataSource != null)
  26.      {
  27.          Table table = new Table();
  28.          Controls.Add(table);
  29.          //選中項索引
  30.          int selectedItemIndex = SelectedIndex;
  31.          //項索引
  32.          int index = 0;
  33.          //項數(shù)量
  34.          count = 0;
  35.          foreach (object dataItem in dataSource)
  36.          {
  37.              ListItemType itemType = ListItemType.Item;
  38.              if (index == selectedItemIndex)
  39.              {
  40.                 
  41.                  itemType = ListItemType.SelectedItem;
  42.              }
  43.              else if (index % 2 != 0)
  44.              {
  45.                  itemType = ListItemType.AlternatingItem;
  46.              }
  47.              //根據(jù)不同項索引創(chuàng)建樣式
  48.              CreateItem(table, index, itemType, useDataSource, dataItem);
  49.              count++;
  50.              index++;
  51.          }
  52.      }
  53.      //執(zhí)行綁定時執(zhí)行時執(zhí)行
  54.      if (useDataSource)
  55.      {
  56.          //保存項數(shù)量
  57.          ViewState["ItemCount"] = ((dataSource != null) ? count : -1);
  58.      }
  59.  }
  60.  //創(chuàng)建項
  61.  private TemplatedListItem CreateItem(Table table,  網(wǎng)站標題:ASP.NET數(shù)據(jù)綁定控件開發(fā)淺析
    網(wǎng)頁URL:http://www.5511xx.com/article/dhseijh.html