CompositeControl 内的数据绑定控件
我创建了一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以继承
CompositeDataBoundControl
而不是CompositeControl
You can inherit
CompositeDataBoundControl
insted ofCompositeControl