一.回顧  如 " /> 国产大片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)系我們修改或刪除,多謝。

主站蜘蛛池模板: 热久久2018亚洲欧美 | 18女下面流水不遮网站免费 | 国产乱辈通伦影片在线播放亚洲 | 国产成人a在一区线观看高清 | 亚洲一品AV片观看五月色婷婷 | 精品午夜视频 | 男人插曲女人的叫声 | 色爱区综合小说 | JAVASCRIPTJAVA水多多 | 绝色娇嫩美人妻老师 | 十大禁止安装的黄台有风险 | 小女生RAPPER入口 | 国产午夜伦伦伦午夜伦 | 免费乱理伦片在线观看夜 | 久拍国产在线观看 | 艳鉧动漫1~6全集观看在线 | 成人在无码AV在线观看一 | 亚洲在线视频自拍精品 | 男女车车的车车网站W98免费 | 国产午夜不卡 | 成人综合在线视频免费观看完整版 | 亚洲欧美一区二区三区蜜芽 | 最新亚洲人成网站在线影院 | 国产精品人妻无码久久久奥特曼 | 日本湿姝在线观看 | 久久精品手机观看 | 日本伦理片 中文字幕 | 久久亚洲午夜牛牛影视 | 午夜一级毛片看看 | 久久只有这里有精品4 | 女人18毛片| 乡村教师电影完整版在线观看 | 久久亚洲精品中文字幕 | 男女午夜性爽快免费视频不卡 | CHINA篮球体育飞机2023 | 国产精品美女久久久网站动漫 | 99久久99久久精品国产片果冻 | 男女作爱在线播放免费网页版观看 | 国产午夜理论片YY8840Y | 寂寞夜晚视频高清观看免费 | 激情内射亚州一区二区三区爱妻 |