[英]Getting Eval to work in a databound, templated custom control
我正在整理一個模板化的數據綁定控件。 目前,它可以使用以下語法...
<cc:ItemChooserControl ID="ItemChooser" runat="server">
<TitleTemplate>
<h4><%# DataBinder.Eval(Container.DataItem, "DisplayName") %></h4>
</TitleTemplate>
</cc:ItemChooserControl>
但我想用較短,更簡單的Eval
代替。
<h4><%# Eval("DisplayName") %></h4>
但是,使用直接Eval時出現的錯誤是:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
我正在將數據綁定到自定義的HtmlTableCell ...
public class TitleTemplateTableCell: HtmlTableCell, INamingContainer
{
private object m_DataItem;
public virtual object DataItem
{
get { return m_DataItem; }
set { m_DataItem = value; }
}
}
使用以下自定義DataBind(刪除了不相關的代碼)...
foreach (object dataItem in dataSource) {
TitleTemplateTableCell title = new TitleTemplateTableCell();
TitleTemplate.InstantiateIn(title); // TitleTemplate is the template property
title.DataItem = dataItem;
title.DataBind();
}
HtmlTable單元格屬於HtmlContainerControl,它不繼承其他數據控件WebControl所做的常規DataBoundControl,例如GridView。 因此,您收到此錯誤; 它不是DataBoundControl。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.