一.回顧  如 " /> 日本男人的天堂,国产精品亚洲一区二区三区,国产精品美女视频

中文字幕日韩一区二区_国产一区二区av_国产毛片av_久久久久国产一区_色婷婷电影_国产一区二区精品

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

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

  一.回顧

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

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

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

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

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

  其他更多的

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

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

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

  2.對(duì)數(shù)據(jù)的某一項(xiàng)進(jìn)行操作: 因?yàn)槎x了Item項(xiàng),那你肯定需要一個(gè)ItemCollection集合,其可以方便的為你檢索數(shù)據(jù)

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

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

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

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

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

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

  3.TemplatedListItem表示TemplatedList的項(xiàng)

  三.編寫TemplatedList

  1.TemplatedList主要功能簡(jiǎn)介

  提供一個(gè)ItemTemplate模板屬性,提供三種不同項(xiàng)樣式,ItemCommand 事件冒泡事件及4個(gè)事件

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

  以下為必須

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

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

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

  (4)定義控件項(xiàng)DataItem,即模板的一個(gè)容器

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

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

  3.具體實(shí)現(xiàn)

  下面我們來具體看實(shí)現(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(
"交替項(xiàng)樣式"),
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(
"一般項(xiàng)樣式"),
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(
"選中項(xiàng)樣式"),
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(
"項(xiàng)模板"),
PersistenceMode(PersistenceMode.InnerProperty),
TemplateContainer(
typeof(TemplatedListItem))
]
public virtual ITemplate ItemTemplate
{
get
{
return itemTemplate;
}
set
{
itemTemplate
= value;
}
}


[
Bindable(
true),
DefaultValue(
-1),
Description(
"選中項(xiàng)索引,默認(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();
}
//獲取上次選中項(xiàng)
int oldSelectedIndex = SelectedIndex;
ViewState[
"SelectedIndex"] = value;

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

//第一次選中項(xiàng)不執(zhí)行
if ((oldSelectedIndex != -1) && (table.Rows.Count > oldSelectedIndex))
{
item
= (TemplatedListItem)table.Rows[oldSelectedIndex];
//判斷項(xiàng)類型,為了將選中項(xiàng)還原為數(shù)據(jù)項(xiàng)
if (item.ItemType != ListItemType.EditItem)
{
ListItemType itemType
= ListItemType.Item;
if (oldSelectedIndex % 2 != 0)
itemType
= ListItemType.AlternatingItem;
item.SetItemType(itemType);
}
}
//第一次執(zhí)行此項(xiàng),并一直執(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)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 国产亚洲精品综合一区 | 色呦呦在线 | 亚洲国产精品一区二区三区 | 欧美一区二区在线播放 | 午夜精品一区二区三区在线观看 | 日本欧美国产在线观看 | 国产电影一区二区在线观看 | 视频一区在线观看 | 91视频亚洲 | 综合成人在线 | 亚洲精品久久嫩草网站秘色 | 喷潮网站 | 精品欧美一区二区三区久久久 | 91九色婷婷 | 91欧美精品 | 在线日韩精品视频 | 欧美国产精品 | 国产精品久久久久久亚洲调教 | 国产在线观看网站 | 国产视频中文字幕在线观看 | 国产成人免费视频网站视频社区 | 国产精品一区二区三级 | 欧美日韩黄色一级片 | 亚洲精品久久久 | 成人午夜精品 | 色婷婷av一区二区三区软件 | 日韩另类 | 精品久久香蕉国产线看观看亚洲 | 97精品视频在线观看 | 欧美无乱码久久久免费午夜一区 | 国产精品久久片 | 国产一区精品在线 | 国产偷录叫床高潮录音 | 精品国产一级片 | 一级免费a | 日日爱av | 日韩中文字幕一区二区 | 中文字幕亚洲国产 | 亚洲国产一区二区三区在线观看 | 欧美国产视频 | 高清国产午夜精品久久久久久 |