在CompositeControl内部绑定控件

本文关键字:控件 绑定 内部 CompositeControl | 更新日期: 2023-09-27 18:14:02

我已经创建了一个CompositeControl,本质上是一个包装器的MultiView,但如果我试图使用任何数据绑定控件,如GridView或FormView内的视图,我得到错误:

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

我已经将类剥离到最小值,但我仍然得到错误。该类看起来像这样:

[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();
    }
}

标记如下:

<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>

如果我将FormView移出CompositeControl,它的数据绑定没有问题。另外,如果我使用标准的MultiView,它工作得很好。

任何想法?提前感谢(第一个帖子,所以抱歉,如果我错过了任何信息)

编辑:为了使事情更加奇怪,如果我提取FormView到一个单独的ascx UserControl,并把它放在视图内,它的工作原理!

在CompositeControl内部绑定控件

可以继承CompositeDataBoundControl而不是CompositeControl