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

推荐订阅源

SecWiki News
SecWiki News
N
Netflix TechBlog - Medium
D
Docker
Stack Overflow Blog
Stack Overflow Blog
云风的 BLOG
云风的 BLOG
U
Unit 42
Recorded Future
Recorded Future
G
Google Developers Blog
T
Threatpost
Google DeepMind News
Google DeepMind News
D
DataBreaches.Net
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
The Blog of Author Tim Ferriss
C
Cyber Attacks, Cyber Crime and Cyber Security
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
GbyAI
GbyAI
V
Vulnerabilities – Threatpost
Project Zero
Project Zero
WordPress大学
WordPress大学
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
M
MIT News - Artificial intelligence
月光博客
月光博客
Spread Privacy
Spread Privacy
Know Your Adversary
Know Your Adversary
人人都是产品经理
人人都是产品经理
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Hacker News
The Hacker News
K
Kaspersky official blog
Simon Willison's Weblog
Simon Willison's Weblog
MongoDB | Blog
MongoDB | Blog
J
Java Code Geeks
I
Intezer
Y
Y Combinator Blog
P
Proofpoint News Feed
G
GRAHAM CLULEY
L
LINUX DO - 热门话题
Schneier on Security
Schneier on Security
B
Blog
Jina AI
Jina AI
V2EX - 技术
V2EX - 技术
雷峰网
雷峰网
Last Week in AI
Last Week in AI
V
V2EX
Martin Fowler
Martin Fowler
P
Palo Alto Networks Blog
Latest news
Latest news
Google DeepMind News
Google DeepMind News
L
Lohrmann on Cybersecurity
The Last Watchdog
The Last Watchdog

博客园 - 大口仔

网上邻居访问提示"未授予用户在此计算机上的请求登录类型"的解决 获取保存在路由器中的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>