Repeater控件綁定的三種方式
方式一
在aspx頁面,寫好需要循環(huán)輸出的內容,一般包含用戶自定義控件、服務器控件、Html格式的片段、和<%# Eval("Name")%>這種方式來動態(tài)顯示獲取到得數(shù)據(jù)列表:
<asp:Repeater ID="rpImage" runat="server">
<ItemTemplate>
<li>
<a href="http://www.dhdzp.com/lmfeng/archive/2012/03/06/<%# (Container.DataItem as ProductImage).ResourceUrl%>" class="<%# Container.ItemIndex == 0 ? "currImg " : "" %>">
<img src="http://www.dhdzp.com/lmfeng/archive/2012/03/06/<%# (Container.DataItem as ProductImage).ResourceUrl%>"
class="<%# (Container.DataItem as ProductImage).ImageVersion)%>">
<%# Eval("Name").ToString() %>
</a>
</li>
</ItemTemplate>
</asp:Repeater>
在cs文件,是用GetProductImageList方法來獲取List<ProductImage>類型的數(shù)據(jù)列表,并綁定在Repeater控件上面:
上面的不包含用戶自定義控件、服務器控件,所以不需要ItemDataBound事件來對單個的數(shù)據(jù)項進行個性化的賦值
protected override void BindDataSource()
{
this.rpImage.DataSource = GetProductImageList();
this.rpImage.DataBind();
}
方式二
在aspx頁面,這次包含了用戶自定義控件,所以需要用到ItemDataBound事件來對列表中的每一個用戶自定義控件進行個性化的賦值,用戶自定義控件可以有公用的方法或者屬性,
讓我們在ItemDataBound事件中賦值:
<asp:Repeater ID="gvItemList" runat="server" EnableViewState="false">
<ItemTemplate>
<li>
<UCCommon:ImageCell ID="imageCell" runat="server" />
<a href="http://www.dhdzp.com/lmfeng/archive/2012/03/06/###" title='<%# Eval("Name").ToString() %>' href='http://www.dhdzp.com/lmfeng/archive/2012/03/06/<%# Eval("Code").ToString()%>'>
<UCCommon:ProductFullNameCell ID="productFullNameCell" runat="server" />
</a>
<UCCommon:UCProductControlCell ID="productControlCell" runat="server"/>
</li>
</ItemTemplate>
</asp:Repeater>
在cs文件,用戶自定義控件可以有公用的方法或者屬性,讓我們在ItemDataBound事件中賦值:
protected override void BindDataSource()
{
this.gvItemList.DataSource = productList;
this.gvItemList.DataBind();
}
protected override void OnInit(EventArgs e)
{
this.gvItemList.ItemDataBound += new RepeaterItemEventHandler(this.OnItemListDataBound);
base.OnInit(e);
}
private void OnItemListDataBound(object sender, RepeaterItemEventArgs e)
{
ProductCellInfo productItem = (ProductCellInfo)e.Item.DataItem;
if (productItem != null)
{
ProductFullNameCell productName;
ImageCell image;
ProductControlCell productControlCell;
foreach (Control sub in e.Item.Controls)
{
productName = sub as ProductFullNameCell;
if (productName != null)
{
productName.InitProductFullName(productItem.Title, productItem.PromotionTitle, DispalyContentLength);
continue;
}
image = sub as ImageCell;
if (image != null)
{
image.InitImageCell2(productItem.ID, productItem.Code, productItem.Name, productItem.ImageUrl, productItem.ImageVersion);
continue;
}
productControlCell = sub as ProductControlCell;
if (productControlCell != null)
{
productControlCell.InitProductControlCell(productItem);
continue;
}
}
}
}
方式三:
在aspx頁面,可以顯示設置OnItemDataBound屬性,就不用像方式二那樣,在cs文件中的OnInit方法中動態(tài)綁定,代碼如下:
<asp:Repeater ID="rptListCell" runat="server" OnItemDataBound="RptAllOnItemDataBound">
<ItemTemplate>
<li>
<a href='http://www.dhdzp.com/lmfeng/archive/2012/03/06/<%#Eval("ID"))>' title='<%#Encode(Eval("Name")) %>'>
<span><%#Encode(Eval("Name")) %></span>
<asp:PlaceHolder ID="pNew" runat="server" Visible="false"></asp:PlaceHolder>
<asp:PlaceHolder ID="pHot" runat="server" Visible="false"></asp:PlaceHolder>
<asp:Literal ID="literalValidGiftOption" runat="server"></asp:Literal>
</a>
</li>
</ItemTemplate>
</asp:Repeater>
在cs文件:
protected override void BindDataSource()
{
base.BindDataSource();
this.rptListCell.DataSource = this.List;
this.rptListCell.DataBind();
}
protected void RptAllOnItemDataBound(object sender, RepeaterItemEventArgs e)
{
CategoryInfo category = (CategoryInfo)e.Item.DataItem;
PlaceHolder pHot = e.Item.FindControl("pHot") as PlaceHolder;
PlaceHolder pNew = e.Item.FindControl("pNew") as PlaceHolder;
Literal lit = e.Item.FindControl("literalValidGiftOption") as Literal;
switch (category.PromotionStatus)
{
case "H":
pHot.Visible = true;
break;
case "N":
pNew.Visible = true;
break;
default:
break;
}
lit.Text = category.Name;
}
- asp.net使用Repeater控件中的全選進行批量操作實例
- ASP.NET中repeater控件用法實例
- 在Repeater控件中通過Eval的方式綁定Style樣式代碼
- Repeater控件與PagedDataSource結合實現(xiàn)分頁功能
- Repeater控件實現(xiàn)編輯、更新、刪除等操作示例代碼
- 給Repeater控件里添加序號的5種才常見方法介紹
- Repeater控件動態(tài)變更列(Header,Item和Foot)信息實現(xiàn)思路
- 如何取得Repeater控件選擇的項目及注意事項
- Repeater控件分別綁定數(shù)組和ArrayList實現(xiàn)思路
- Repeater控件數(shù)據(jù)導出Excel(附演示動畫)
- 淺析Repeater控件的使用 (原樣導出和動態(tài)顯示/隱藏Repeater中的列)

