一、綁定Repeater控制項的資料來源
aspx.cs檔案中綁定Repeater控制項的資料來源在BindDataSource()中:
protected override void BindDataSource()
{
this.rpID.DataSource = this.dataList;
this.rpID.DataBind();
}
Repeater控制項事件OnItemDataBound,表示在迴圈載入<ItemTemplate>列表時候,會對每一項Item進行具體的操作。
例子:
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;
}
}
可以看出來對於Repeater控制項中的<ItemTemplate>裡面的使用者控制項、伺服器控制項的賦值是在Repeater控制項事件OnItemDataBound中進行的。
二、使用者控制項
使用者控制項T.ascx代碼有:
欄位:
private string title;
屬性:
public string Title
{
get
{
return this.title;
}
}
方法:
SetTitle()
{
this.title = "Hello world!";
}
其實這裡的欄位賦值也是可以用屬性進行賦值的,不過要在屬性聲明中加上set部分。
那麼這個T的使用者控制項的要是在Repeater控制項中的<ItemTemplate>裡面出現的話,是要在
Repeater控制項事件OnItemDataBound裡面進行具體的賦值的。
例子:
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);
}
}
可以看到上面的操作已經把資料存到title欄位裡面了,在使用者空間的T.ascx代碼中用<%=Title%>來訪問Title屬性,
因為之前已經聲明了Title屬性的get部分。
三、一個簡單的Reapter控制項的使用全貌
在aspx頁面中:
<asp:Repeater ID="rpID" runat="server" OnItemDataBound="rp_ItemDataBound">
<ItemTemplate>
<asp:Literal ID="lt" runat="server"></asp:Literal>
</ItemTemplate>
</asp:Repeater>
在aspx.cs頁面中:
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;
}
}
注意我前面已經提到了伺服器控制項和使用者控制項在這裡面的使用方式其實是一摸一樣的。
可以看出來BindDataSource()是對aspx頁面中最外層的使用者控制項、伺服器控制項的資料來源進行綁定,
而OnItemDataBound事件是對Repeater控制項中的<ItemTemplate>裡面的使用者控制項、伺服器控制項的資料來源
進行綁定。
四、在Repeater控制項中嵌套使用Repeater控制項
這其實是一個特例:
在aspx頁面中:
<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頁面中:
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();
}
}
五、一般使用者控制項的賦值情況
在aspx頁面中:
<UCCommon:T ID="UCT" runat="server" />
在aspx.cs頁面中:
protected override void BindDataSource()
{
this.UCT.ItemList = dataList;
this.UCT.Title = "hello world!";
}
是在給使用者控制項的屬性賦值。
使用者控制項隱藏檔案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;
}
}
這裡面就是使用者控制項定義了屬性對欄位進行賦值的。
如果使用者控制項中還有使用者控制項的賦值情況:
使用者控制項隱藏檔案T.ascx.cs代碼有:
protected override void BindDataSource()
{
T_T.SelectList = dataList;
T_T.Title = "hello world!";
}
這裡和aspx頁面一樣是在BindDataSource()中對外層的使用者控制項進行賦值的。
當然T_T中會聲明相應的欄位和屬性儲存資料。
最後會在自己的BindDataSource()裡面再次賦值給需要的控制項,或者直接在頁面上顯示所得的資料。
可以在使用者控制項的T.ascx代碼中直接擷取屬性例如:
<%= Title %>
在aspx頁面中Repeater控制項中的<ItemTemplate>裡面綁定資料:
<%#Eval("Detail") == null || Eval("Detail").ToString() == string.Empty ? " " : Eval("Detail")%>
<%#(Container.DataItem as BannerInfo).BannerTitle %>
在aspx頁面中Repeater控制項中Repeater控制項的<ItemTemplate>裡面綁定資料:
<%#Container.DataItem == null || Container.DataItem.ToString()==string.Empty ? " " : Container.DataItem%>
在aspx頁面直接賦值和在使用者控制項中一樣:
<%= Title %>