我创建了一个 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 并将其放入 View 中,它就可以工作!