Repeater中的数据绑定。C#

来源:互联网 发布:u盘数据如何恢复 编辑:程序博客网 时间:2024/05/02 03:05

在aspx.cs中

页面aspx中

可以用

这两种绑定方式都可以。但是有点区别。

网站http://weblogs.asp.net/rajbk/archive/2004/07/20/what-s-the-deal-with-databinder-eval-and-container-dataitem.aspx中的解释比较详细。

来外的解释:

DataBinder.Eval uses reflection to get a value of a property of the
dataitem object. This is less performant than casting the dataitem
object to its actual type, and then access its property. The only
reason to use DataBinder.Eval is that the syntax may be/look simpler.

I always cast it to its actual type, which means it looks like this:
<%# ((MyObject)Container.DataItem).MyProperty %>
Or in case you are binding against a datatable, the dataitem is of the
the type datarowview:
<%# ((DataRowView)Container.DataItem)["MyField"] %>

 

The DataItem property returns a reference to an object, and
System.Object doesn't have an indexer defined. You'd have to cast the
DataItem return value to the proper type before the first expression
would compile.

DataBinder.Eval works because it will use reflection to dig out the
property with the name of "Author" dynamically. The second expression
would be the one to use in this case because it is the only one to
work.

原创粉丝点击