2010-06-22 147 views
1

我有我的自定义控件的formview内。此自定义控件具有多种属性,其中之一是Value,我试图将其绑定。ASP.NET自定义控件 - DataBinding

使用Container.DataItem我可以我的数据绑定控件,一切正常

<fc:Literal runat="server" ID="readState" Label="State:" Value='<%# Container.DataItem("ActivityState") %>' /> 

后来,当我尝试使用Eval数据绑定,那么它不会:

<fc:Literal runat="server" ID="readState" Label="State:" Value='<%# Eval("ActivityState") %>' /> 

提供错误:

​​

一切工作都很好Container.DataItem,所以我的问题是:为什么Container.DataItem工作和Eval不?

回答

2

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.

1

您需要实现/延长下面让数据绑定表达式语法的服务器控件之一:

模板化数据绑定控件: 夫妇的文章: http://msdn.microsoft.com/en-us/library/aa478964.aspx http://msdn.microsoft.com /en-us/library/aa479322.aspx

CompositeDataboundControl:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.compositedataboundcontrol.aspx

DatBoundControl:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.databoundcontrol.aspx