Repeater控件绑定的三种方式

所属分类: 软件编程 / C#教程 阅读数: 53
收藏 0 赞 0 分享

方式一
在aspx页面,写好需要循环输出的内容,一般包含用户自定义控件、服务器控件、Html格式的片段、和<%# Eval("Name")%>这种方式来动态显示获取到得数据列表:

复制代码 代码如下:

<asp:Repeater ID="rpImage" runat="server">
    <ItemTemplate>   
        <li>
            <a href="https://www.jb51.net/lmfeng/archive/2012/03/06/<%# (Container.DataItem as ProductImage).ResourceUrl%>" class="<%# Container.ItemIndex == 0 ? "currImg " : "" %>">
                <img src="https://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="https://www.jb51.net/lmfeng/archive/2012/03/06/###" title='<%# Eval("Name").ToString() %>' href='https://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='https://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;
}

更多精彩内容其他人还在看

C#中Datetimepicker出现问题的解决方法

这篇文章主要给大家介绍了关于C#中Datetimepicker出现问题的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

C# SQLite数据库入门使用说明

这篇文章主要给大家介绍了关于C#中SQLite数据库入门使用的相关资料,文中通过图文以及示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

C#实现批量下载图片到本地示例代码

这篇文章主要给大家介绍了关于C#如何实现批量下载图片到本地的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用c#具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

如何获取C#中方法的执行时间以及其代码注入详解

这篇文章主要给大家介绍了关于如何获取C#中方法的执行时间以及其代码注入的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧
收藏 0 赞 0 分享

C#中通过LRU实现通用高效的超时连接探测

这篇文章主要介绍了c#中通过LRU实现通用高效的超时连接探测,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
收藏 0 赞 0 分享

如何使用C#将Tensorflow训练的.pb文件用在生产环境详解

这篇文章主要给大家介绍了关于如何使用C#将Tensorflow训练的.pb文件用在生产环境的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

C#程序启动项的设置方法

这篇文章主要为大家详细介绍了C#程序启动项的设置方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

c#爬虫爬取京东的商品信息

这篇文章主要给大家介绍了关于利用c#爬虫爬取京东商品信息的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们随着小编来一起学习学习吧
收藏 0 赞 0 分享

C#随机数生成字母金字塔

这篇文章主要为大家详细介绍了C#随机数生成字母金字塔,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

WPF实现窗体中的悬浮按钮

这篇文章主要为大家详细介绍了WPF实现窗体中的悬浮按钮,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多