简体   繁体   English

CompositeControl内部的数据绑定控件

[英]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: 我创建了一个CompositeControl,它实际上是MultiView的包装器,但是如果我尝试在View中使用任何数据绑定控件(例如GridView或FormView),则会收到错误消息:

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. 如果将FormView移到CompositeControl之外,则可以毫无问题地进行数据绑定。 Also If I use a standard MultiView it works fine. 另外,如果我使用标准的MultiView,则效果很好。

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! 编辑:让事情变得更加奇怪,如果我将FormView提取到一个单独的ascx UserControl中并将其放入View中,它将起作用!

您可以继承CompositeDataBoundControl插入的CompositeControl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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