Repeater基础

在aspx文件中加入Repeater 控件,在<ItemTemplate></ItemTemplate>包含的范围里加入自己控制的代码,需要替换的变量使用<%# Eval("SellerName")%>;注意两侧的引号。

.aspx:

<asp:Repeater ID="SellerRpt" runat="server">
<ItemTemplate>
<li><a href='<%# Eval("SellerName")%>' target="_blank">
<%# Eval("ComName")%></a></li>
</ItemTemplate>
</asp:Repeater>
对应的后台cs中,在页面加载处加入数据绑定的代码: protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = SellerDA.GetTopHotSellers();
SellerRpt.DataSource = dt;
SellerRpt.DataBind();
}
}
aspx中"SellerName"、"ComName"为DataTable 中的列名。 优化 直接使用DataItem可减少Eval函数的执行步骤,优化页面解析时间: <%# ((DataRowView)Container.DataItem)["SellerName"]%>替换<%# Eval("SellerName")%> <%--其他绑定方法,可以对没有列明如数组进行绑定--%>
<%#Container.DataItem %>
<%--绑定格式等--%> <%#Eval("times","{0:yyyy-MM-dd}")%>
<%#Eval("price","{C:货币}")%> ArrayList数据源 如果数据源是ArrayList,并且ArrayList为一列string数组,则可不用写出列名: .aspx: <asp:Repeater ID="topAdHintRpt" runat="server">
<ItemTemplate>
<asp:Label ID="BarLabel" CssClass="bar" runat="server" Text="|"></asp:Label>
<a href="#"><span>
<%#Container.DataItem%></span></a>
</ItemTemplate>
</asp:Repeater>
.cs: ArrayList alterText;
AdDA.GetIndexTopList(out alterText);
topAdHintRpt.DataSource = alterText;
topAdHintRpt.DataBind(); 处理后显示 某些情况下,数据库中检索出来的数据并不适合直接显示出来,想要适当处理后显示(eg:日期的格式,字符串长度的控制),可使用标签来占位,在onitemdatabound函数中自行控制: .aspx: <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="ProRpt_ItemDataBound">
<ItemTemplate>
<asp:Label ID="colinDate" runat="server" Text=""></asp:Label>
</ItemTemplate>
</asp:Repeater>
.cs: protected void ProRpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView rowv = (DataRowView)e.Item.DataItem;//找到分类Repeater关联的数据项
string strDate = rowv["clDate"].ToString();
Label DateLB = e.Item.FindControl("colinDate") as Label;
DateLB.Text = strDate.Substring(, );
}
} 嵌套Reapeter的显示 对于某些复杂的显示逻辑,需用用到Reapeter的嵌套,这里需要自行控制2层数据源的数据绑定: .aspx: <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="ProRpt_ItemDataBound">
<ItemTemplate>
<asp:Repeater ID="ParaRpt" runat="server" OnItemDataBound="ParaRpt_ItemDataBound">
<ItemTemplate>
<asp:Label ID="bar" CssClass="bar" runat="server" Text="|"></asp:Label>
<span class="para">
<%# Eval("Name")%>:
<%# Eval("Value")%></span>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
.cs: protected void ProRpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
//判断里层repeater处于外层repeater的哪个位置( AlternatingItemTemplate,FooterTemplate,
//HeaderTemplate,,ItemTemplate,SeparatorTemplate
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rep = e.Item.FindControl("ParaRpt") as Repeater;//找到里层的repeater对象
DataRowView rowv = (DataRowView)e.Item.DataItem;//找到分类Repeater关联的数据项
string str = Convert.ToString(rowv["Pro_Content"]); //获取填充子类的内容
rep.DataSource = Product.FillPara(str);
rep.DataBind();
}
} //三重绑定可以在二重绑定方法中加入事件
 rep.ItemDataBound += new RepeaterItemEventHandler(rpt_ItemDataBound);

转自:http://www.cnblogs.com/me115/archive/2011/04/09/2010682.html

Repeater实现数据绑定的更多相关文章

  1. Repeater数据绑定和操作

    Repeater使用详细指南 ASP.NET WebForm开发中尽量少用系统提供的runat="server"的服务器控件,尤其像GridView之类的“重量级”武器,自动生成的 ...

  2. ASP.NET服务器控件数据绑定总结

    using System; using System.Collections.Generic; using System.Text; using System.Web.UI.WebControls;/ ...

  3. 20150301—ASP.NET的Repeater

    Repeater与GridView等数据列表一样,都是用来显示数据库的信息的,其中Repeater是最基本的列表形式,其用法也比较灵活. 一.Repeater的位置: 工具箱-数据-Repeater ...

  4. ASP.NET数据绑定控件简介

    •数据绑定分为数据源和数据绑定控件两部分(①数据绑定控件通过数据源获取和修改数据②数据绑定控件通过数据源隔离数据提供者和数据使用者)数据绑定控件→数据源→数据库•数据源:SqlDataSource(连 ...

  5. 嵌套repeater

    通过外层repeater的值来进行内层repeater的数据绑定 前台代码部分: <asp:repeater runat="server" id="repeater ...

  6. asp.net动态网站repeater控件使用及分页操作介绍

    asp.net动态网站repeater控件使用及分页操作介绍 1.简单介绍 Repeater 控件是一个容器控件,可用于从网页的任何可用数据中创建自定义列表.Repeater 控件没有自己内置的呈现功 ...

  7. webform repeater 的使用

    1  repeater    定义: 重复器    根据数据库里的内容将repeater里的内容重复赋值 ,在itemtemplate下   配合<%# Eval(" ") ...

  8. ASP.NET入门(class0612)

    内容:掌握基于ASP.Net的Web开发,B/S结构原理.ASP.Net内部原理.状态管理(Cookie.Session.ViewState等).数据验证.普通ASP.Net控件.母版.ListVie ...

  9. 动态从数据库读取菜单(ASP.NET版)

    这几天一直打算做个从数据读取导航菜单的效果,以前做的时候都是写死的(太死了),好了话不多说,先看效果! 我是个小菜,高手请不要喷!我在网上查了好久,说用menu控件,但是我用了不太好!最后我决定用re ...

随机推荐

  1. LP64是什么意思

    在64位机器上,如果int是32位,long是64位,pointer也是64位,那么该机器就是LP64的,其中的L表示Long,P表示Pointer,64表示Long和Pointer都是64位的.由于 ...

  2. YUI Array 之dedupe(快速去重)

    YUI.Array.dedupe函数,如果传参为有length属性,返回一个去除掉重复项('1’ 与1 | true 与'true’认为相等)的参数数组副本,如果传参的length为undefined ...

  3. ActionSupport.getText()方法 以及 js中:<s:text name="" />

    下面略述com.opensymphony.xwork2.ActionSupport.getText()方法 public String getText(String aTextName) 说明:Get ...

  4. nginx+keepalived+tomcat之tomcat性能调优

    body{ font-family: Nyala; font-size: 10.5pt; line-height: 1.5;}html, body{ color: ; background-color ...

  5. HDU ACM 题目分类

    模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 104 ...

  6. 在Docker上部署使用Azure CLI镜像

    Docker是非常流行的容器技术,在Docker中安装部署多种工具非常快速和方便:而Azure CLI是微软提供的可以在Linux/Mac上运行的跨平台命令行管理工具,本文介绍如何在Azure上安装部 ...

  7. 有意思的GacUI

    所有方法,无论是你写还是工具来codegen还是用宏,最终都指向把这些名字和对应的指针存在一个map里.C++是不提供这个功能的,我也没仔细研究过qt怎么做,不过我在我自己的gacui里面实现了类似的 ...

  8. Match类

    Regex在匹配的时候,常常会返回一个Match类的对象,今天就来看看怎么玩这个对象. 一.属性 Captures 按从里到外.从左到右的顺序获取由捕获组匹配的所有捕获的集合(如果正则表达式用 Reg ...

  9. 《编程珠玑》第二章 aha算法

    A题:给定一个最多包含40亿个随机排列的32位整数的顺序文件,找出一个不在文件中的32位整数. 1.在文件中至少存在这样一个数? 2.如果有足够的内存,如何处理? 3.如果内存不足,仅可以用文件来进行 ...

  10. invesments 第三章 上

    1. How firms issue securities: 公司如何发行股票 A.       primary market: 新的股票,债券和其他的证券第一次发行的market B.        ...