惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Darknet – Hacking Tools, Hacker News & Cyber Security
N
News and Events Feed by Topic
N
News | PayPal Newsroom
SecWiki News
SecWiki News
P
Privacy International News Feed
T
Troy Hunt's Blog
Attack and Defense Labs
Attack and Defense Labs
N
News and Events Feed by Topic
L
LINUX DO - 热门话题
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Security Latest
Security Latest
AWS News Blog
AWS News Blog
S
Secure Thoughts
W
WeLiveSecurity
H
Heimdal Security Blog
T
Threat Research - Cisco Blogs
I
Intezer
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Security @ Cisco Blogs
G
GRAHAM CLULEY
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Spread Privacy
Spread Privacy
L
Lohrmann on Cybersecurity
C
CERT Recently Published Vulnerability Notes
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
Google Online Security Blog
Google Online Security Blog
Cisco Talos Blog
Cisco Talos Blog
雷峰网
雷峰网
Cloudbric
Cloudbric
Y
Y Combinator Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园_首页
Hacker News: Ask HN
Hacker News: Ask HN
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Google DeepMind News
Google DeepMind News
Vercel News
Vercel News
云风的 BLOG
云风的 BLOG
Latest news
Latest news
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
D
Docker
Recent Announcements
Recent Announcements
博客园 - 【当耐特】
H
Help Net Security
博客园 - 司徒正美
TaoSecurity Blog
TaoSecurity Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Check Point Blog
博客园 - 叶小钗

博客园 - 大口仔

网上邻居访问提示"未授予用户在此计算机上的请求登录类型"的解决 获取保存在路由器中的ADSL账号和密码 GX DM800遥控TV设置 硅钢片铁芯、坡莫合金、非晶及纳米晶软磁合金 在HTML中显示回车和空格 - 大口仔 - 博客园 n!和 Fibnoacci函数的递归与非递归 SQL Server和Oracle的常用函数对比 自动配置IE代理脚本 - 大口仔 - 博客园 DD-WRT 上 VPN 的设置 - 大口仔 css文本 jQuery插件 用户控件路径的解决方案 - 大口仔 - 博客园 CSS属性大全 关闭excel进程 c# 枚举基础 与 枚举属性的访问 首字母大写 SQL Server 2005之PIVOT/UNPIVOT行列转换 Excel导出方法总结
数据绑定以及Container.DataItem - 大口仔 - 博客园
大口仔 · 2009-06-02 · via 博客园 - 大口仔

灵活的运用数据绑定操作
        绑定到简单属性:<%#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(Container.DataItem,"字段名","格式")%>

        绑定到DataReader:
        <%#((IDataReader)Container.DataItem).字段名%>

        当然为了方便一般使用最多的就是DataBinder类的eval方法了.不过这样对于同时要绑定大量的数据效率要低一些

在绑定数据时经常会用到这个句程序:<%# DataBinder.eval(Container.DataItem,"xxxx")%>或者<%# DataBinder.eval(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(Container.DataItem,"Name")和Container.DataItem("Name")有什么区别?

DataBinder是System.Web里面的一个静态类,它提供了eval方法用于简化数据绑定表达式的编写,但 是它使用的方式是通过Reflection等开销比较大的方法来达到易用性,因此其性能并不是最好的。而Container则根本不是任何一个静态的对象 或方法,它是ASP.NET页面编译器在数据绑定事件处理程序内部声明的局部变量,其类型是可以进行数据绑定的控件的数据容器类型(如在Repeater 内部的数据绑定容器叫RepeaterItem),在这些容器类中基本都有DataItem属性,因此你可以写Container.DataItem,这 个属性返回的是你正在被绑定的数据源中的那个数据项。如果你的数据源是DataTable,则这个数据项的类型实际是DataRowView。


如果绑定到IList<T>时,则用
<%# ((SysUser)Container.DataItem).LoginName%>

.Net2.0以上,可以如下配置web.config:

<configuration>
<system.web>
 <pages>
    <namespaces>
                <add namespace ="System.Data" />
                <add namespace="System.Text"/>
                <add namespace="Kilo.Model"/>
            </namespaces>

        </pages>
</system.web>
</configuration>