一、Repeater绑定ArrayList数据源
Repeater绑定ArrayList数据源 就相当与把ArrayList的数据绑定到Repeater的ItemTemplate 上 就是RepeaterItem等于ArrayList的一项例如
MessageBox MBx = new MessageBox(); 'MessageBox 为一个类
ArrayList lst = new ArrayList();
lst.Add(MBx);
Repeater1.DataSource = lst;
Repeater1.DataBind();
这样 在页面上
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand" >
<ItemTemplate>
<div style="width:200px; height:100px; background-color:Green;"><%#Container.DataItem%>>
</div>
</ItemTemplate>
</asp:Repeater>
就会输出MessageBox 类名称
说明 RepeaterItem就相当于 MBx
看 MSDN 上讲,可以作为数据绑定控件的数据源对象需要继承并实现 IEnumerable 或 IListSource 接口。
而 Dictionary 字典类很明显是继承并实现了 IEnumerable 接口的,那么如何将一个 Dictionary 对象绑定到 Repeater 等控件呢?
Dictionary 绑定如下:
代码
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("1","张三");
dictionary.Add("2", "李四");
dictionary.Add("3", "王五");
代码
<form runat="server">
<asp:Repeater id="List" runat="server">
<HeaderTemplate>
<table border="1" width="100%">
<tr>
<th>站长</th>
<th>网站</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#eval_r("key")%></td>
<td><%#eval_r("value")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
如果使用的是 ArrayList 等类时,绑定时,直接用 <%# Container.DataItem%> 就可以了。
灵活的运用数据绑定操作
绑定到简单属性:<%#UserName%>
绑定到集合:<asp:ListBox id="ListBox1" datasource='<%# myArray%>' runat="server">
绑定到表达式:<%#(class1.property1.ToString() + "," + class1.property2.ToString())%>
绑定到方法返回值:<%# GetSafestring(str) %>
绑定到Hashtable:<%# ((DictionaryEntry)Container.DataItem).Key%>
绑定到ArrayList:<%#Container.DataItem %>
若数组里里放的是对象则可能要进行必要的转换后再绑定如:
<%#((对象类型)Container.DataItem).属性%>
绑定到DataView,DataTable,DataSet:
<%#((DataRowView)Container.DataItem)["字段名"]%>或
<%#((DataRowView)Container.DataItem).Rows[0]["字段名"]%>
要格式化则:
<%#string.Format("格式",((DataRowView)Container.DataItem)["字段名"])%>
<%#DataBinder.eval_r(Container.DataItem,"字段名","格式")%>
绑定到DataReader:
<%#((IDataReader)Container.DataItem).字段名%>
当然为了方便一般使用最多的就是DataBinder类的Eval方法了.不过这样对于同时要绑定大量的数据效率要低一些
在绑定数据时经常会用到这个句程序:<%# DataBinder.eval_r(Container.DataItem,"xxxx")%>或者<%# DataBinder.eval_r(Container,"DataItem.xxxx")%>
今天又学到一种,而且微软也说这种方法的效率要比以上两种高。
<%# ((DataRowView)Container.DataItem)["xxxx"]%>
很有用的,这样可以在前台页面做好多事情了。
还要记住要这样用必须要在前台页面导入名称空间System.Data,否则会生成错误信息。
<%@ Import namespace="System.Data" %>
这种用法其实和<%# ((DictionaryEntry)Container.DataItem).Key%>是一个道理。
Text='<%# DataBinder.eval_r(Container.DataItem, "字段") %>' 这样的方法是最快的
Text='<%# GetPrice() %>' 也可以绑定方法,但方法要是public的
Text='<%# "CarDetails.aspx?CarID=" + DataBinder.eval_r(Container.DataItem, "CarID") %>' 还可以连接多个字段
关键是Container这个东西,它比较神秘。它的名称空间是System.ComponentModel。
初学.NET,现在在看DataGrid控件,在ItemTemplate显示数据时,
DataBinder.eval_r(Container.DataItem,"Name")和Container.DataItem("Name")有什么区别?DataBinder是
System.Web里面的一个静态类,它提供了Eval方法用于简化数据绑定表达式的编写,但是它使用的方式是通过Reflection等开销比
较大的方法来达到易用性,因此其性能并不是最好的。而Container则根本不是任何一个静态的对象或方法,它是ASP.NET页面编译
器在数据绑定事件处理程序内部声明的局部变量,其类型是可以进行数据绑定的控件的数据容器类型(如在Repeater内部的数据绑定
容器叫RepeaterItem),在这些容器类中基本都有DataItem属性,因此你可以写Container.DataItem,这个属性返回的是你正在
被绑定的数据源中的那个数据项。如果你的数据源是DataTable,则这个数据项的类型实际是DataRowView。
相关推荐
//将控件Repeater中的数据导出到Excel或Word中 //将下列代码放入需要导出数据的.aspx文件对应的.cs文件中 // private void Export(String FileType, String FileName) { Response.Clear(); Response....
c#Repeater多层嵌套的代码 1.找到分类Repeater关联的数据项 2.根据分类ID查询该分类下的产品,并绑定产品Repeater
代码可直接复制粘贴使用,用c#做的,对于不想写代码的人不错的选择
Repeater DataList GridView 如何进行数据绑定 新增 删除 修改 查询
.aspx页面代码 代码如下: ”Repeater1″ runat=”server”> <ItemTemplate> <%# ((KeyValuePair<int>>)Container.DataItem).Key %> ”Repeater2″ runat=”server” xss=removed>>)Container.DataItem)....
.net 中用repeater做分页转页 repeater绑定数据
一般开发绑定数据的时候,Repeater控件是非常好用的控件,但是很多.net开发人员苦恼于无法分页,于是开发一个基于Repeater的分页控件,现在拿出来和大家一起分享分享。写得不好大家可以尽管拍砖,源码在本文后面。...
常用的C#数据绑定控件有:Repeater、DataList、GridView、DetailsView等,在这里我拿Repeater来简单说明问题。使用该属性指定用来填充Repeater控件的数据源。DataSource可以是任何System.Collections.IEnumerable对象...
1、在C# 中连接数据库。如下图:2、在项目中添加新建项,建立一个数据集,并把Categories从服务器资源列表中拖到这个数据集模板中并点击菜单“生成-生成解决方案”,如下图: 3、在aspx的webform上放一个...
前台代码: 代码如下: ”rptarry” runat=”server”> <HeaderTemplate><table></HeaderTemplate> <ItemTemplate> <tr><td> <%# GetDataItem()%> </td></tr> </ItemTemplate> <FooterTemplate></table>...
如为GridView、DataList以及Repeater等数据绑定控件实现分页、呈现自定义的分页数据以及制作图片浏览程序等,因为AspNetPager控件和数据是独立的,因此要分页的数据可以来自任何数据源,如SQL Server、Oracle、...
控件绑定repeater控件,稍微修改一下可以绑定Datalist等数据控件。。 DAL层的对数据库连接操作就不上传了。。。 本来想动态创建lnkbtn 再填加 EventHandler。。 由于回传每次重新要new链接按钮 还要数据绑定 .....
.NET中的Repeater控件支持数据模板,而且可以自由地定义样式,这里我们就来详解ASP.NET数据绑定操作中Repeater控件的用法,需要的朋友可以参考下
动态加载网站首页的菜单并绑定菜单的子菜单,其中使用到嵌套Repeater的用法
0445 使用GridView Web服务器控件绑定数据源 246 0446 使用GridView Web服务器控件自定义外观风格 247 0447 使用GridView Web服务器控件的简单排序 247 0448 如何在GridView Web服务器控件中分页 248 0449 ...
如为GridView、DataList以及Repeater等数据绑定控件实现分页、呈现自定义的分页数据以及制作图片浏览程序等,因为AspNetPager控件和数据是独立的,因此要分页的数据可以来自任何数据源,如SQL Server、Oracle、...
Repeater控件是一个数据绑定容器控件,它能够生成各个项的列表,并可以使用模板定义网页上各个项的布局。当该页运行时,该控件为数据源中的每个项重复此布局。 配合模板使用repeater控件 若要使用repeater控件,需...
本文实例讲述了asp.net实现DataList与Repeater嵌套绑定的方法。分享给大家供大家参考,具体如下: <%@ Page Language=C# AutoEventWireup=true CodeFile=home.aspx.cs Inherits=home %> <body> <...
0445 使用GridView Web服务器控件绑定数据源 246 0446 使用GridView Web服务器控件自定义外观风格 247 0447 使用GridView Web服务器控件的简单排序 247 0448 如何在GridView Web服务器控件中分页 248 ...