Three ways to bind Repeater controls
- 2020-05-10 18:41:05
- OfStack
Method 1
On the aspx page, write the content that needs to be output in a loop, including user-defined controls, server controls, Html fragments, and
<
%# Eval("Name")%
>
This way to dynamically display the list of acquired data:
<asp:Repeater ID="rpImage" runat="server">
<ItemTemplate>
<li>
<a href="//www.ofstack.com/lmfeng/archive/2012/03/06/<%# (Container.DataItem as ProductImage).ResourceUrl%>" class="<%# Container.ItemIndex == 0 ? "currImg " : "" %>">
<img src="//www.ofstack.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>
In the cs file, the GetProductImageList method is used to get List < ProductImage > A list of data types bound to the Repeater control:
The above does not contain user-defined controls, server controls, so there is no need for ItemDataBound events to personalize individual data items
protected override void BindDataSource()
{
this.rpImage.DataSource = GetProductImageList();
this.rpImage.DataBind();
}
Way 2
On the aspx page, the user-defined control is included this time, so it is necessary to use the ItemDataBound event to assign personalized values to each user-defined control in the list. The user-defined control can have common methods or properties.
Let's assign values in the ItemDataBound event:
<asp:Repeater ID="gvItemList" runat="server" EnableViewState="false">
<ItemTemplate>
<li>
<UCCommon:ImageCell ID="imageCell" runat="server" />
<a href="//www.ofstack.com/lmfeng/archive/2012/03/06/###" title='<%# Eval("Name").ToString() %>' href='//www.ofstack.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>
In the cs file, user-defined controls can have common methods or properties, let's assign values in the ItemDataBound event:
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;
}
}
}
}
Method 3:
On the aspx page, you can set the OnItemDataBound property without the dynamic binding in the OnInit method in the cs file as in mode 2. The code is as follows:
<asp:Repeater ID="rptListCell" runat="server" OnItemDataBound="RptAllOnItemDataBound">
<ItemTemplate>
<li>
<a href='//www.ofstack.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>
In cs file:
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;
}