ASP.NET自定义控件,是否可以限制模板的内容?
某些 ASP.NET 控件仅允许某些类型的项目,例如:
<uc:MyControl runat="server"....>
<MyTableTemplate>
<MyTableRow .... />
<MyTableRow .... />
<MyTableRow .... />
</MyTableTemplate>
</uc:MyControl>
我尝试在自己的自定义控件中重新创建此控件,这样您只能将 MyTableRow 对象放入模板中,而 Visual Studio 将阻止设计器将其他任何内容放入模板中我
一直在查看 ControlBuilder类,但我不完全确定它能达到我想要的效果。此外,这些示例没有提到我上面的嵌套,其中 MyTableRow 数组位于另一个模板字段中。
有谁知道如何实现这一目标?
编辑:UpdatePanel 似乎是这样工作的:如果您像这样声明一个 UpdatePanel:
<asp:UpdatePanel>
<Triggers>
</Triggers>
</asp:UpdatePanel>
除了 intellisense 中列出的两个特定控件之外,您不能在“触发器”部分中放置任何内容,如果尝试,您会收到设计时错误。这正是我想要模仿的行为。
====
我试图实现亚历克斯的建议,这是我的代码:
public partial class MyControl : System.Web.UI.UserControl
{
[PersistenceMode(PersistenceMode.InnerProperty)]
public ActionButtonCollection ActionButtons
{
get;
set;
}
}
public class ActionButtonCollection : Collection<ActionButton>
{
}
public abstract class ActionButtonBase
{
public string Test { get; set; }
}
public class ActionButton : ActionButtonBase
{
}
但是,这个代码里面没有智能感知
<uc:MyCustomControl runat="server">
<ActionButtons>
</ActionButtons>
</uc:MyCustomControl >
Some ASP.NET controls only permit items of certain types, for example:
<uc:MyControl runat="server"....>
<MyTableTemplate>
<MyTableRow .... />
<MyTableRow .... />
<MyTableRow .... />
</MyTableTemplate>
</uc:MyControl>
I'm trying to recreate this in my own custom control, such that you can only put MyTableRow objects into the template and Visual Studio will prevent the designer putting anything else in.
I've been looking at the ControlBuilder class but I am not fully certain it does what I want. In addition the examples do not mention the nesting I have above where the array of MyTableRow is within another template field.
Does anyone know how to achieve this?
Edit: The UpdatePanel seems to work this way: If you declare an UpdatePanel like this:
<asp:UpdatePanel>
<Triggers>
</Triggers>
</asp:UpdatePanel>
You cannot put anything in the Triggers section other than the two specific controls listed in intellisense, and you get a design-time error if you try. This is the exact behaviour I wish to mimic.
====
I tried to implement Alex's suggestion, here is my code:
public partial class MyControl : System.Web.UI.UserControl
{
[PersistenceMode(PersistenceMode.InnerProperty)]
public ActionButtonCollection ActionButtons
{
get;
set;
}
}
public class ActionButtonCollection : Collection<ActionButton>
{
}
public abstract class ActionButtonBase
{
public string Test { get; set; }
}
public class ActionButton : ActionButtonBase
{
}
However with this code there is no intellisense inside
<uc:MyCustomControl runat="server">
<ActionButtons>
</ActionButtons>
</uc:MyCustomControl >
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:添加了执行此操作的示例的链接到谷歌文档。
首先,构建网站,然后您就可以通过 IntelliSense 使用此控件。
实际上,
UpdatePanel
的
属性不是模板。它只是对象的集合。您可以通过声明您自己的类和继承Collection
的类来实现此目的;然后只需将Collection
类型的公共属性添加到您的控件中,您将能够存档“更新面板触发器”行为。这是一个示例:
最后,您的控件:
然后您可以在页面上使用它:
顺便说一句,除了从
UpdatePanelTrigger
继承的类型之外,您不能将任何内容放入Triggers
部分代码>类。EDIT: added a link to the example of doing this to google docs.
Firstly, build the website and then you'll be able use this control with IntelliSense.
Actually,
<Triggers>
property ofUpdatePanel
is not a template. It is just a collection of objects. You could achieve this by declaring you own class and a class which inheritCollection<YourClass>
; then just add the public property ofCollection<YourClass>
type to your control and you will be able to archive the "update panel triggers" behaviour.Here is an example:
and finally, your control:
then you could use it on your page:
BTW, you cannot put anything to the
Triggers
section other than types which inherit from theUpdatePanelTrigger
class.