一.回顧  如 " /> 免费看黄视频网站,99精品国产成人一区二区在线,成人在线欧美

一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

asp.net控件開發(fā)基礎(chǔ)(17)

  本篇將開始介紹如自定義數(shù)據(jù)綁定控件,這里感謝很多人的支持,有你們的支持很高興。這里首先需要大家熟悉ASP.NET模板控件的使用,還有自定義模板控件.因為數(shù)據(jù)綁定控件多是基于模板控件的.

  一.回顧

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

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

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

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

  這三步應(yīng)該是必須要做的

  其他更多的

  你可能需要對綁定的數(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ù)具體需求來定.但一個完成的控件是需要的。

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

  這次的demo為不完整的Datalist控件,來源還是MSDN的例子,我們命名為TemplatedList,此控件未定義ItemCollection集合,好了,根據(jù)上面的分析我們先為TemplatedList提供項和委托及為事件提供數(shù)據(jù)的幾個EventArgs,請看下面類圖

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

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

  3.TemplatedListItem表示TemplatedList的項

  三.編寫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 方法及復(fù)合控件相關(guān)方法(模板控件為特殊的復(fù)合控件)

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

  3.具體實現(xiàn)

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

  (1)定義控件成員屬性

#region 靜態(tài)變量

private static readonly object EventSelectedIndexChanged = new object();
private static readonly object EventItemCreated = new object();
private static readonly object EventItemDataBound = new object();
private static readonly object EventItemCommand = new object();
#endregion

#region 成員變量
private IEnumerable dataSource;
private TableItemStyle itemStyle;
private TableItemStyle alternatingItemStyle;
private TableItemStyle selectedItemStyle;
private ITemplate itemTemplate;
#endregion

#region 控件屬性

[
Category(
"Style"),
Description(
"交替項樣式"),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
NotifyParentProperty(
true),
PersistenceMode(PersistenceMode.InnerProperty),
]
public virtual TableItemStyle AlternatingItemStyle
{
get
{
if (alternatingItemStyle == null)
{
alternatingItemStyle
= new TableItemStyle();
if (IsTrackingViewState)
((IStateManager)alternatingItemStyle).TrackViewState();
}
return alternatingItemStyle;
}
}


[
Category(
"Style"),
Description(
"一般項樣式"),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
NotifyParentProperty(
true),
PersistenceMode(PersistenceMode.InnerProperty),
]
public virtual TableItemStyle ItemStyle
{
get
{
if (itemStyle == null)
{
itemStyle
= new TableItemStyle();
if (IsTrackingViewState)
((IStateManager)itemStyle).TrackViewState();
}
return itemStyle;
}
}

[
Category(
"Style"),
Description(
"選中項樣式"),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
NotifyParentProperty(
true),
PersistenceMode(PersistenceMode.InnerProperty),
]
public virtual TableItemStyle SelectedItemStyle
{
get
{
if (selectedItemStyle == null)
{
selectedItemStyle
= new TableItemStyle();
if (IsTrackingViewState)
((IStateManager)selectedItemStyle).TrackViewState();
}
return selectedItemStyle;
}
}



[
Bindable(
true),
Category(
"Appearance"),
DefaultValue(
-1),
Description(
"The cell padding of the rendered table.")
]
public virtual int CellPadding
{
get
{
if (ControlStyleCreated == false)
{
return -1;
}
return ((TableStyle)ControlStyle).CellPadding;
}
set
{
((TableStyle)ControlStyle).CellPadding
= value;
}
}

[
Bindable(
true),
Category(
"Appearance"),
DefaultValue(
0),
Description(
"The cell spacing of the rendered table.")
]
public virtual int CellSpacing
{
get
{
if (ControlStyleCreated == false)
{
return 0;
}
return ((TableStyle)ControlStyle).CellSpacing;
}
set
{
((TableStyle)ControlStyle).CellSpacing
= value;
}
}



[
Bindable(
true),
Category(
"Appearance"),
DefaultValue(GridLines.None),
Description(
"The grid lines to be shown in the rendered table.")
]
public virtual GridLines GridLines
{
get
{
if (ControlStyleCreated == false)
{
return GridLines.None;
}
return ((TableStyle)ControlStyle).GridLines;
}
set
{
((TableStyle)ControlStyle).GridLines
= value;
}
}

[
Bindable(
true),
Category(
"Data"),
DefaultValue(
null),
Description(
"數(shù)據(jù)源"),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]
public IEnumerable DataSource
{
get
{
return dataSource;
}
set
{
dataSource
= value;
}
}


[
Browsable(
false),
DefaultValue(
null),
Description(
"項模板"),
PersistenceMode(PersistenceMode.InnerProperty),
TemplateContainer(
typeof(TemplatedListItem))
]
public virtual ITemplate ItemTemplate
{
get
{
return itemTemplate;
}
set
{
itemTemplate
= value;
}
}


[
Bindable(
true),
DefaultValue(
-1),
Description(
"選中項索引,默認為-1")
]
public virtual int SelectedIndex
{
get
{
object o = ViewState["SelectedIndex"];
if (o != null)
return (int)o;
return -1;
}
set
{
if (value < -1)
{
throw new ArgumentOutOfRangeException();
}
//獲取上次選中項
int oldSelectedIndex = SelectedIndex;
ViewState[
"SelectedIndex"] = value;

if (HasControls())
{
Table table
= (Table)Controls[0];
TemplatedListItem item;

//第一次選中項不執(zhí)行
if ((oldSelectedIndex != -1) && (table.Rows.Count > oldSelectedIndex))
{
item
= (TemplatedListItem)table.Rows[oldSelectedIndex];
//判斷項類型,為了將選中項還原為數(shù)據(jù)項
if (item.ItemType != ListItemType.EditItem)
{
ListItemType itemType
= ListItemType.Item;
if (oldSelectedIndex % 2 != 0)
itemType
= ListItemType.AlternatingItem;
item.SetItemType(itemType);
}
}
//第一次執(zhí)行此項,并一直執(zhí)行
if ((value != -1) && (table.Rows.Count > value))
{
item
= (TemplatedListItem)table.Rows[value];
item.SetItemType(ListItemType.SelectedItem);
}
}
}
}


#endregion

NET技術(shù)asp.net控件開發(fā)基礎(chǔ)(17),轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 草草草在线 | 日本韩国理论片大全在线 | 碰91精品国产91久久婷婷 | 深爱五月激情网 | 日本大片免a费观看视频+播放器 | 国产日产欧产精品推荐推荐 | 国农村精品国产自线拍 | 一本三道a无线码一区v小说 | 伊人精品国产 | 久久久久久一级毛片免费无遮挡 | 六月婷婷久香在线视频 | 久久久久精彩视频 | 91视频啪啪 | 精品视频在线看 | 91短视频版在线观看www | 播放久久国产乱子伦精品 | 久久涩精品| 加勒比一区 | 亚洲人成网男女大片在线播放 | 亚洲第一香蕉视频 | 亚洲成a人片77777群色 | 婷婷亚洲综合五月天小说在线 | 日韩天天摸天天澡天天爽视频 | 午夜精品一区二区三区在线观看 | 国产一区美女 | 婷婷六月综合 | 91嫩草国产线免费观看 | 日韩一区国产二区欧美三区 | 日本高清一区二区三区水蜜桃 | 精品国精品自拍自在线 | 日韩亚洲一区中文字幕在线 | 色婷在线 | 一本色道久久88综合亚洲精品高清 | 黄色网在线免费观看 | a男人的天堂久久a毛片 | 中文字幕日韩三级 | 午夜视频在线免费观看 | 成人精品视频在线观看播放 | 亚洲国产成人久久笫一页 | 97夜色| 婷婷久久综合九色综合九七 |