ASP.NET自定义控件-数据绑定
我在Formview中有自定义控件.此自定义控件具有多种属性,其中之一是我试图进行数据绑定的 Value
.
I have my custom control inside of a formview. This custom control has a variety of properties one of which is Value
which I am trying to databind.
使用 Container.DataItem
,我可以对控件进行数据绑定,并且一切正常:
Using Container.DataItem
I can databind my control, and everything works:
<fc:Literal runat="server" ID="readState" Label="State:" Value='<%# Container.DataItem("ActivityState") %>' />
然后,当我尝试使用 Eval
进行数据绑定时,却没有:
Then when I try to databind using Eval
, then it doesn't:
<fc:Literal runat="server" ID="readState" Label="State:" Value='<%# Eval("ActivityState") %>' />
给出错误:
诸如Eval(),XPath()和Bind()之类的数据绑定方法只能在数据绑定控件的上下文中使用.
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
一切与 Container.DataItem
一起工作都很好,所以我的问题是:为什么 Container.DataItem
起作用而 Eval
不起作用?
Everything workds great with Container.DataItem
, so my question is: Why does Container.DataItem
work and Eval
doesn't?
评估只能与模板控件一起使用.
Eval can only be used with templated controls.
Eval方法评估后期绑定模板中的数据表达式数据绑定控件,例如GridView,DetailsView和FormView控件.在运行时,评估方法调用Eval(Object,String)DataBinder对象的方法,引用当前的数据项命名容器.命名容器通常是最小的数据绑定控件的一部分包含整个记录,例如一行在GridView控件中.你可以因此,仅将Eval方法用于在模板的内部绑定数据绑定控件.
The Eval method evaluates late-bound data expressions in the templates of data-bound controls such as the GridView, DetailsView, and FormView controls. At run time, the Eval method calls the Eval(Object, String) method of the DataBinder object, referencing the current data item of the naming container. The naming container is generally the smallest part of the data-bound control that contains a whole record, such as a row in a GridView control. You can therefore use the Eval method only for binding inside templates of a data-bound control.