在aspx.cs中
页面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.