views:

21

answers:

1

Context

I'm putting together a templated, databound control. Presently it works with the following syntax...

<cc:ItemChooserControl ID="ItemChooser" runat="server">
    <TitleTemplate>
        <h4><%# DataBinder.Eval(Container.DataItem, "DisplayName") %></h4>
    </TitleTemplate>
</cc:ItemChooserControl>

Problem

What I would like though is that the shorter, simpler Eval would work instead.

<h4><%# Eval("DisplayName") %></h4>

What I get however when using straight Eval is an error:

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

Code

I'm databinding to a custom HtmlTableCell...

public class TitleTemplateTableCell: HtmlTableCell, INamingContainer
{
    private object m_DataItem;

    public virtual object DataItem
    {
        get { return m_DataItem; }
        set { m_DataItem = value; }
    }
}

With the following custom DataBind (non-related code removed)...

foreach (object dataItem in dataSource) {
    TitleTemplateTableCell title = new TitleTemplateTableCell();
    TitleTemplate.InstantiateIn(title);    // TitleTemplate is the template property
    title.DataItem = dataItem;
    title.DataBind();
 }
A: 

The HtmlTable cell belongs to the HtmlContainerControl which does not inherit from the general DataBoundControl that the other data control WebControls do, such as GridView. Hence, you are receiving this error; it is not a DataBoundControl.

TheGeekYouNeed
Hmm ok so I unsuccessfully played around with what I thought the fix would be. How exactly do I fix it?
T. Stone
Read about creating your own custom databound controls
TheGeekYouNeed
http://msdn.microsoft.com/en-us/library/ms366539.aspx
TheGeekYouNeed