VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • C#教程之Repeater控件绑定的三种方式

方式一
在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;
}



相关教程