CompositeControl 内的数据绑定控件

发布于 2024-12-14 19:53:53 字数 1745 浏览 0 评论 0原文

我创建了一个 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 之外,它的数据绑定不会出现问题。另外,如果我使用标准的多视图,它工作得很好。

有什么想法吗?提前致谢(第一篇文章,如果我错过了任何信息,请道歉)

编辑:让事情变得更奇怪的是,如果我将 FormView 提取到单独的 ascx UserControl 中并将其放入视图中,它就可以工作!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

下雨或天晴 2024-12-21 19:53:53

您可以继承 CompositeDataBoundControl 而不是 CompositeControl

You can inherit CompositeDataBoundControl insted of CompositeControl

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文