一、綁定Repeater控件的數據源
aspx.cs文件中綁定Repeater控件的數據源在BindDataSource()中:protected override void BindDataSource()
{
this.rpID.DataSource = this.dataList;
this.rpID.DataBind();
}
Repeater控件事件OnItemDataBound,表示在循環(huán)加載<ItemTemplate>列表時(shí)候,會(huì )對每一項Item進(jìn)行具體的操作。
例子: Protected void rp_ItemDataBound(object sender,RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
List<Info> Infos = this.rpID.DataSource as List<Info>;
Info info = e.Item.DataItem as Info; Literal lt = e.Item.FindControl("lt") as Literal; lt.Text = Info.Title;
}
}可以看出來(lái)對于Repeater控件中的<ItemTemplate>里面的用戶(hù)控件、服務(wù)器控件的賦值是在Repeater控件事件OnItemDataBound中進(jìn)行的。二、用戶(hù)控件用戶(hù)控件T.ascx代碼有:字段:
private string title;屬性:
public string Title
{
get
{
return this.title;
}
}方法:
SetTitle()
{
this.title = "Hello world!";
}
其實(shí)這里的字段賦值也是可以用屬性進(jìn)行賦值的,不過(guò)要在屬性聲明中加上set部分。
那么這個(gè)T的用戶(hù)控件的要是在Repeater控件中的<ItemTemplate>里面出現的話(huà),是要在
Repeater控件事件OnItemDataBound里面進(jìn)行具體的賦值的。
例子:
Protected void rp_ItemDataBound(object sender,RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
List<Info> Infos = this.rpID.DataSource as List<Info>;
Info info = e.Item.DataItem as Info;
T t = e.Item.FindControl("t") as T;t.SetTitle(info.Title);
}
}
可以看到上面的操作已經(jīng)把數據存到title字段里面了,在用戶(hù)空間的T.ascx代碼中用<%=Title%>來(lái)訪(fǎng)問(wèn)Title屬性,
因為之前已經(jīng)聲明了Title屬性的get部分。
三、一個(gè)簡(jiǎn)單的Reapter控件的使用全貌在aspx頁(yè)面中:<asp:Repeater ID="rpID" runat="server" OnItemDataBound="rp_ItemDataBound">
<ItemTemplate>
<asp:Literal ID="lt" runat="server"></asp:Literal>
</ItemTemplate>
</asp:Repeater>在aspx.cs頁(yè)面中:protected override void BindDataSource()
{
this.rpID.DataSource = this.dataList;
this.rpID.DataBind();
} Protected void rp_ItemDataBound(object sender,RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
List<Info> Infos = this.rpID.DataSource as List<Info>;
Info info = e.Item.DataItem as Info; Literal lt = e.Item.FindControl("lt") as Literal; lt.Text = Info.Title;
}
}
注意我前面已經(jīng)提到了服務(wù)器控件和用戶(hù)控件在這里面的使用方式其實(shí)是一摸一樣的。
可以看出來(lái)BindDataSource()是對aspx頁(yè)面中最外層的用戶(hù)控件、服務(wù)器控件的數據源進(jìn)行綁定,
而OnItemDataBound事件是對Repeater控件中的<ItemTemplate>里面的用戶(hù)控件、服務(wù)器控件的數據源
進(jìn)行綁定。四、在Repeater控件中嵌套使用Repeater控件這其實(shí)是一個(gè)特例:
在aspx頁(yè)面中:<asp:Repeater ID="rpID" runat="server" OnItemDataBound="rp_ItemDataBound">
<ItemTemplate>
<asp:Repeater ID="rp_rpID" runat="server">
<asp:Literal ID="lt" runat="server"></asp:Literal>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>在aspx.cs頁(yè)面中:protected override void BindDataSource()
{
this.rpID.DataSource = this.dataList;
this.rpID.DataBind();
} Protected void rp_ItemDataBound(object sender,RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
List<Info> Infos = this.rpID.DataSource as List<Info>;
Info info = e.Item.DataItem as Info; Repeater rp_rpID = e.Item.FindControl("rp_rpID") as Repeater; rp_rpID.DataSource = dataList; rp_rpID.DataBind();
}
} 五、一般用戶(hù)控件的賦值情況在aspx頁(yè)面中:<UCCommon:T ID="UCT" runat="server" />在aspx.cs頁(yè)面中:protected override void BindDataSource()
{
this.UCT.ItemList = dataList;
this.UCT.Title = "hello world!";
}
是在給用戶(hù)控件的屬性賦值。用戶(hù)控件隱藏文件T.ascx.cs代碼有:字段:private List<Info> itemList;
private string title;屬性:public string Title
{
set
{
this.title = value;
}
}
public List<Info> ItemList
{
set
{
this.itemList = value;
}
}這里面就是用戶(hù)控件定義了屬性對字段進(jìn)行賦值的。如果用戶(hù)控件中還有用戶(hù)控件的賦值情況:用戶(hù)控件隱藏文件T.ascx.cs代碼有:protected override void BindDataSource()
{
T_T.SelectList = dataList;
T_T.Title = "hello world!";
}
這里和aspx頁(yè)面一樣是在BindDataSource()中對外層的用戶(hù)控件進(jìn)行賦值的。當然T_T中會(huì )聲明相應的字段和屬性保存數據。
最后會(huì )在自己的BindDataSource()里面再次賦值給需要的控件,或者直接在頁(yè)面上顯示所得的數據。可以在用戶(hù)控件的T.ascx代碼中直接獲取屬性例如:<%= Title %>在aspx頁(yè)面中Repeater控件中的<ItemTemplate>里面綁定數據:<%#Eval("Detail") == null || Eval("Detail").ToString() == string.Empty ? " " : Eval("Detail")%><%#(Container.DataItem as BannerInfo).BannerTitle %>在aspx頁(yè)面中Repeater控件中Repeater控件的<ItemTemplate>里面綁定數據:<%#Container.DataItem == null || Container.DataItem.ToString()==string.Empty ? " " : Container.DataItem%>在aspx頁(yè)面直接賦值和在用戶(hù)控件中一樣:<%= Title %>
聯(lián)系客服