Repeater控件绑定的三种方式

所属分类: 软件编程 / C#教程 阅读数: 83
收藏 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# SendInput 模拟鼠标操作的实现方法

C# SendInput 模拟鼠标操作的实现方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#中 paint()与Onpaint()的区别

paint是事件onpaint方法onpaint方法是调用paint事件的,用哪一个,效果是一样,就看那一个方便了内部是这样实现的:
收藏 0 赞 0 分享

c#中GetType()与Typeof()的区别

c#中GetType()与Typeof()的区别,需要的朋友可以参考一下
收藏 0 赞 0 分享

将字符串转换成System.Drawing.Color类型的方法

将字符串转换成System.Drawing.Color类型的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# 抓取网页内容的方法

C# 抓取网页内容的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

基于C#后台调用跨域MVC服务及带Cookie验证的实现

本篇文章介绍了,基于C#后台调用跨域MVC服务及带Cookie验证的实现。需要的朋友参考下
收藏 0 赞 0 分享

使用C#获取远程图片 Form用户名与密码Authorization认证的实现

本篇文章介绍了,使用C#获取远程图片 Form用户名与密码Authorization认证的实现。需要的朋友参考下
收藏 0 赞 0 分享

Winform跨线程操作的简单方法

线程间操作无效:从不是创建控件“label1”的线程访问它
收藏 0 赞 0 分享

C# WINFORM 强制让窗体获得焦点的方法代码

C# WINFORM 强制让窗体获得焦点的方法代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#中方括号[]的语法及作用介绍

C#中方括号[]可用于数组,索引、属性,更重要的是用于外部DLL类库的引用。
收藏 0 赞 0 分享
查看更多