Databinding controls inside CompositeControl

494 Views Asked by At

I have created a CompositeControl that is essentially a wrapper for a MultiView, but if I try to use any databound controls such as GridView or FormView inside the View, I get the error:

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

I've stripped the class down to the bare minimum and I'm still getting the error. The class looks like this:

[DefaultProperty("Pages"), ParseChildren(true, "Pages"), ToolboxData("<{0}:TestTabs runat=\"server\"></{0}:TestTabs>")]
public class TestTabs : CompositeControl {

    private MultiView _multiViewControl;

    private Collection<View> _pages;
    public Collection<View> Pages {
        get {
            if (_pages == null) _pages = new Collection<View>();
            return _pages;
        }
    }

    protected override void CreateChildControls() {
        _multiViewControl = new MultiView();
        foreach (View page in Pages) { _multiViewControl.Views.Add(page); }
        if (_multiViewControl.Views.Count > 0) _multiViewControl.ActiveViewIndex = 0;

        this.Controls.Add(_multiViewControl);

        base.CreateChildControls();
    }
}

And the markup is as follows:

<cc:TestTabs ID="testTabs" runat="server">
    <asp:View runat="server">
        <asp:FormView ID="fvTest" runat="server">
            <ItemTemplate>
                <asp:Label ID="lblTest" runat="server" Text='<%#Eval("TestField")%>' />
            </ItemTemplate>
        </asp:FormView>
    </asp:View>
</cc:TestTabs>

If I move the FormView outside the CompositeControl, it databinds with no problem. Also If I use a standard MultiView it works fine.

Any ideas? Thanks in advance (first post, so apologies if I've missed any info)

Edit: To make things even more strange, if I extract the FormView into a separate ascx UserControl and put that inside the View, it works!

1

There are 1 best solutions below

1
On

You can inherit CompositeDataBoundControl insted of CompositeControl