用户控件和服务器控件的数据绑定
生活随笔
收集整理的這篇文章主要介紹了
用户控件和服务器控件的数据绑定
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、綁定Repeater控件的數(shù)據(jù)源
aspx.cs文件中綁定Repeater控件的數(shù)據(jù)源在BindDataSource()中: protected override void BindDataSource()
{
?? this.rpID.DataSource = this.dataList;
?? this.rpID.DataBind();
}
Repeater控件事件OnItemDataBound,表示在循環(huán)加載<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>里面的用戶控件、服務(wù)器控件的賦值是在Repeater控件事件OnItemDataBound中進行的。 二、用戶控件 用戶控件T.ascx代碼有: 字段:
private string title; 屬性:
public string Title
{
??? get
??? {
?????? return this.title;
??? }
} 方法:
SetTitle()
{
??? this.title = "Hello world!";
}
其實這里的字段賦值也是可以用屬性進行賦值的,不過要在屬性聲明中加上set部分。
那么這個T的用戶控件的要是在Repeater控件中的<ItemTemplate>里面出現(xiàn)的話,是要在
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);
????? }
? }
可以看到上面的操作已經(jīng)把數(shù)據(jù)存到title字段里面了,在用戶空間的T.ascx代碼中用<%=Title%>來訪問Title屬性,
因為之前已經(jīng)聲明了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;
????? }
? }
注意我前面已經(jīng)提到了服務(wù)器控件和用戶控件在這里面的使用方式其實是一摸一樣的。
可以看出來BindDataSource()是對aspx頁面中最外層的用戶控件、服務(wù)器控件的數(shù)據(jù)源進行綁定,
而OnItemDataBound事件是對Repeater控件中的<ItemTemplate>里面的用戶控件、服務(wù)器控件的數(shù)據(jù)源
進行綁定。 四、在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()中對外層的用戶控件進行賦值的。 當(dāng)然T_T中會聲明相應(yīng)的字段和屬性保存數(shù)據(jù)。
最后會在自己的BindDataSource()里面再次賦值給需要的控件,或者直接在頁面上顯示所得的數(shù)據(jù)。 可以在用戶控件的T.ascx代碼中直接獲取屬性例如: <%= Title %> 在aspx頁面中Repeater控件中的<ItemTemplate>里面綁定數(shù)據(jù): <%#Eval("Detail") == null || Eval("Detail").ToString() == string.Empty ? " " : Eval("Detail")%> <%#(Container.DataItem as BannerInfo).BannerTitle %> 在aspx頁面中Repeater控件中Repeater控件的<ItemTemplate>里面綁定數(shù)據(jù): <%#Container.DataItem == null || Container.DataItem.ToString()==string.Empty ? " " : Container.DataItem%> 在aspx頁面直接賦值和在用戶控件中一樣: <%= Title %>
轉(zhuǎn)載于:https://www.cnblogs.com/splendidme/archive/2011/10/05/2199497.html
總結(jié)
以上是生活随笔為你收集整理的用户控件和服务器控件的数据绑定的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 好听的中文名字女孩
- 下一篇: 萃开头的成语有哪些啊?