简体   繁体   中英

Databinding controls inside CompositeControl

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!

您可以继承CompositeDataBoundControl插入的CompositeControl

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM