2010-06-22 146 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