ASP.NET自定义控件,是否可以限制模板的内容?

发布于 2024-10-20 06:01:22 字数 1464 浏览 1 评论 0原文

某些 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 技术交流群。

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

发布评论

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

评论(1

灰色世界里的红玫瑰 2024-10-27 06:01:22

编辑:添加了执行此操作的示例的链接到谷歌文档

首先,构建网站,然后您就可以通过 IntelliSense 使用此控件。

实际上,UpdatePanel 属性不是模板。它只是对象的集合。您可以通过声明您自己的类和继承 Collection 的类来实现此目的;然后只需将 Collection 类型的公共属性添加到您的控件中,您将能够存档“更新面板触发器”行为。

这是一个示例:

public abstract class UpdatePanelTrigger
{
    public string ControlId { get; set; }
}

public class UpdatePanelTrigger1 : UpdatePanelTrigger
{

}

public class UpdatePanelTrigger2 : UpdatePanelTrigger
{

}

public class UpdatePanelTriggerCollection : Collection<UpdatePanelTrigger>
{

}

最后,您的控件:

public class MyControl : WebControl
{
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public UpdatePanelTriggerCollection Triggers { get; set; }
}

然后您可以在页面上使用它:

<my:MyControl runat="Server">
    <Triggers>
        <my:UpdatePanelTrigger1 ControlId="ctrl1" />
        <my:UpdatePanelTrigger2 ControlId="ctrl2" />
    </Triggers>
</my:MyControl>

顺便说一句,除了从 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 of UpdatePanel is not a template. It is just a collection of objects. You could achieve this by declaring you own class and a class which inherit Collection<YourClass>; then just add the public property of Collection<YourClass> type to your control and you will be able to archive the "update panel triggers" behaviour.

Here is an example:

public abstract class UpdatePanelTrigger
{
    public string ControlId { get; set; }
}

public class UpdatePanelTrigger1 : UpdatePanelTrigger
{

}

public class UpdatePanelTrigger2 : UpdatePanelTrigger
{

}

public class UpdatePanelTriggerCollection : Collection<UpdatePanelTrigger>
{

}

and finally, your control:

public class MyControl : WebControl
{
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public UpdatePanelTriggerCollection Triggers { get; set; }
}

then you could use it on your page:

<my:MyControl runat="Server">
    <Triggers>
        <my:UpdatePanelTrigger1 ControlId="ctrl1" />
        <my:UpdatePanelTrigger2 ControlId="ctrl2" />
    </Triggers>
</my:MyControl>

BTW, you cannot put anything to the Triggers section other than types which inherit from the UpdatePanelTrigger class.

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