Repeater控制項綁定的三種方式

來源:互聯網
上載者:User

方式一
在aspx頁面,寫好需要迴圈輸出的內容,一般包含使用者自訂控制項、伺服器控制項、Html格式的片段、和<%# Eval("Name")%>這種方式來動態顯示擷取到得資料列表:

複製代碼 代碼如下:<asp:Repeater ID="rpImage" runat="server">
<ItemTemplate>
<li>
<a href="http://www.jb51.net/lmfeng/archive/2012/03/06/<%# (Container.DataItem as ProductImage).ResourceUrl%>" class="<%# Container.ItemIndex == 0 ? "currImg " : "" %>">
<img src="http://www.jb51.net/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>類型的資料列表,並綁定在Repeater控制項上面:
上面的不包含使用者自訂控制項、伺服器控制項,所以不需要ItemDataBound事件來對單個的資料項目進行個人化的賦值複製代碼 代碼如下: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.jb51.net/lmfeng/archive/2012/03/06/###" title='<%# Eval("Name").ToString() %>' href='http://www.jb51.net/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方法中動態綁定,代碼如下:複製代碼 代碼如下:<asp:Repeater ID="rptListCell" runat="server" OnItemDataBound="RptAllOnItemDataBound">
<ItemTemplate>
<li>
<a href='http://www.jb51.net/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;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.