一.回顧  如 " /> 国产大片51精品免费观看,中文字幕人成乱码熟女APP,免费观看成人毛片

天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

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ù)進(jìn)行統(tǒng)一的一些操作(如時間格式化),或者對數(shù)據(jù)的某一項進(jìn)行操作(對某一項進(jìn)行格式化),或者需要觸發(fā)模板控件內(nèi)的一些事件(如databound事件)。

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

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

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

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

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

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

  這次的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ù)合控件)

  當(dāng)然還有其他額外的屬性,樣式,事件

  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(
"選中項索引,默認(rèn)為-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)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 久久AV国产麻豆HD真实乱 | 色欲AV精品人妻一区二区麻豆 | 97超碰97资源在线观看 | 欧美91精品久久久久网免费 | jk制服啪啪网站 | 亚洲欧美精品无码大片在线观看 | 欧美性XXXXX极品娇小 | 91成品视频 | 亚洲欧美强伦一区二区另类 | 亚洲天堂av2017 | 伦理片午夜在线视频 | 美女教师朝桐光在线播放 | 亚洲熟伦熟女专区 | 青青草原亚洲 | 男人有噶坏| 丝袜美女被啪啪不带套漫画 | 欧式午夜理伦三级在线观看 | 乌克兰xxxxx| 一边喂奶一边做边爱 | xxxxx69hd杨幂| 国产午夜理论片YY8840Y | 欧美成人3d动漫专区 | 亚洲伊人久久网 | 少妇两个奶头喷出奶水了怎么办 | 亚洲欧美日韩一级特黄在线 | 午夜伦理一yy4480影院 | 牛牛在线(正)精品视频 | 一个色夫导航 | 青柠视频在线观看高清HD | 亚洲熟妇色自偷自拍另类 | 妺妺窝人体色WWW偷窥女厕 | 亚洲AV久久久久久久无码 | 蓝男色gay| 日韩熟女精品一区二区三区 | 日本邪恶少女漫画大全 | 久久国产精品永久免费网站 | 88蜜桃人妻无码精品系列 | 在线免费观看国产 | 美女张开大腿 | 挺进老师的紧窄小肉六电影完整版 | 精品国产乱码久久久久久下载 |