List作为GridView数据源时,绑定模板列的例子

List<string> attributes = new List<string>();
【转载】List作为GridView数据源时,绑定模板列的例子
narf.DataSource 
= attributes;
narf.DataBind();


.aspx

<asp:gridview ID="narf" runat="server">
 
<Columns>
 
<asp:TemplateField>
 
<ItemTemplate>
 
<asp:TextBox ID="nasdc" runat="server" Text=<%Container.DataItem %>></asp:TextBox>
 
</ItemTemplate>
 
</asp:TemplateField>
 
</Columns>
</asp:gridview>




http://www.cnblogs.com/stu-acer/archive/2009/03/23/1419582.html



http://forums.asp.net/t/1050997.aspx





灵活的运用数据绑定操作
 
绑定到简单属性:<%#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%>
是一个道理。


绑定到DataSet、DataTable时:


<%#((System.Data.DataRowView)Container.DataItem)["字段名"]%>
<%#((System.Data.DataRowView)Container.DataItem)[索引]%>


绑定到DataReader时:
<%#((System.Data.Common.DbDataRecord)Container.DataItem)[索引]%>
<%#((System.Data.Common.DbDataRecord)Container.DataItem)["字段名"]%>


关键是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。
http://blog.csdn.net/jelink/article/details/1118839

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值