从 ASP.NET 标记绑定相关控件
我有一个看起来像这样的控件:
public class MyControl : Control
{
[Bindable(true)]
public MyControl MyControlParent { get; set; }
// snip
}
在我的标记中,我想要以下内容:
<tag:MyControl ID="foo" runat="server">
<tag:MyControl ID="bar" MyControlParent="foo" runat="server"></tag:MyControl>
</tag:MyControl>
我希望能够像这样命名父级的原因是因为我还希望能够执行类似这样的操作: 或者
<tag:MyControl ID="foo" runat="server">
<tag:MyControl ID="foobar" runat="server">
<tag:MyControl ID="bar" MyControlParent="foo" runat="server"></tag:MyControl>
</tag:MyControl>
</tag:MyControl>
或者
<tag:MyControl ID="foo" runat="server"></tag:MyControl>
<!-- snip -->
<tag:MyControl ID="bar" MyControlParent="foo" runat="server"></tag:MyControl>
我尝试添加类型转换器,但它在设置 HttpContext.Current.Handler 之前触发,这意味着我无法在页面控件中搜索所需的 MyControl 实例。此控件中的父子关系赋予属性值一定的继承性,并且并不(不应该)必然暗示控件树中的父子关系。
这个问题有(实际的)解决方案吗?或者我只是想要比现在更多的解决方案? =)
干杯
// 丹尼尔
I have a control that looks along the lines of this:
public class MyControl : Control
{
[Bindable(true)]
public MyControl MyControlParent { get; set; }
// snip
}
In my markup I want the following:
<tag:MyControl ID="foo" runat="server">
<tag:MyControl ID="bar" MyControlParent="foo" runat="server"></tag:MyControl>
</tag:MyControl>
The reason for me wanting to be able to name a parent like this is because I would also like to be able to do something like this:
or
<tag:MyControl ID="foo" runat="server">
<tag:MyControl ID="foobar" runat="server">
<tag:MyControl ID="bar" MyControlParent="foo" runat="server"></tag:MyControl>
</tag:MyControl>
</tag:MyControl>
or
<tag:MyControl ID="foo" runat="server"></tag:MyControl>
<!-- snip -->
<tag:MyControl ID="bar" MyControlParent="foo" runat="server"></tag:MyControl>
I've tried adding a type converter, but it fires before HttpContext.Current.Handler is set which means I can't search the page controls for the desired MyControl instance. Parent-child relationship in this control imparts certain inheritance of property values and doesn't (read shouldn't) necessarily imply a parent-child relationship in the control tree.
Is there a (practical) solution to this problem or am I just wanting more than is possible right now? =)
Cheers
// Daniel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
代码的基本问题是,您将 MyControlParent 属性设置为父控件的 ID,而不是控件本身,但该属性是用 MyControl 类型声明的。可以将属性的类型更改为字符串,然后在代码的其他位置从 ID 查找实际的控件。
The basic issue with your code is that you are setting the MyControlParent property to the ID of the parent control not to the control itself, yet the property is declared with type MyControl. It would be OK to change the type of your property to a string, and then, elsewhere in your code, look up the actual control from the ID.