ASP的嵌套标签的自定义用户控件

问题描述 投票:9回答:4

我刚开始使用C#中自定义用户控件,如果有任何的例子在那里的如何写一个它接受嵌套的标签我不知道?

例如,当你创建一个asp:repeater可以为itemtemplate添加一个嵌套的标签。

c# .net asp.net custom-server-controls
4个回答
16
投票

我前段时间写了一篇关于这个blog post。总之,如果您有以下标记控制:

<Abc:CustomControlUno runat="server" ID="Control1">
    <Children>
        <Abc:Control1Child IntegerProperty="1" />
    </Children>
</Abc:CustomControlUno>

你需要的代码的控制是沿着线:

[ParseChildren(true)]
[PersistChildren(true)]
[ToolboxData("<{0}:CustomControlUno runat=server></{0}:CustomControlUno>")]
public class CustomControlUno : WebControl, INamingContainer
{
    private Control1ChildrenCollection _children;

    [PersistenceMode(PersistenceMode.InnerProperty)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public Control1ChildrenCollection Children
    {
        get
        {
            if (_children == null)
            {
                _children = new Control1ChildrenCollection();
            }
            return _children;
        }
    }
}

public class Control1ChildrenCollection : List<Control1Child>
{
}

public class Control1Child
{
    public int IntegerProperty { get; set; }
}

6
投票

我跟Rob的博客文章,并提出了一个稍微不同的控制。控制是有条件的,真的就像一个if子句:

<wc:PriceInfo runat="server" ID="PriceInfo">
    <IfDiscount>
        You don't have a discount.
    </IfDiscount>
    <IfNotDiscount>
        Lucky you, <b>you have a discount!</b>
    </IfNotDiscount>
</wc:PriceInfo>

在代码我然后设置控制到一个布尔值,它决定哪个子句被渲染的HasDiscount属性。

从Rob的解决方案最大的不同,就是在控制范围内的条款确实可以容纳任意的HTML / ASPX代码。

这里是控制的代码:

using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebUtilities
{
    [ToolboxData("<{0}:PriceInfo runat=server></{0}:PriceInfo>")]
    public class PriceInfo : WebControl, INamingContainer
    {
        private readonly Control ifDiscountControl = new Control();
        private readonly Control ifNotDiscountControl = new Control();

        public bool HasDiscount { get; set; }

        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public Control IfDiscount
        {
            get { return ifDiscountControl; }
        }

        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public Control IfNotDiscount
        {
            get { return ifNotDiscountControl; }
        }

        public override void RenderControl(HtmlTextWriter writer)
        {
            if (HasDiscount)
                ifDiscountControl.RenderControl(writer);
            else
                ifNotDiscountControl.RenderControl(writer);
        }
    }
}

2
投票

我结束了Rob东西通过wayback archive(在@gudmundur-h)非常相似的答案,但我用ITemplate摆脱在使用那恼人的“X标记之间,您不能把内容”的。我不能完全确定什么是真正需要与否,所以这一切都在这里是以防万一。

部分/用户控制标记:mycontrol.ascx

需要注意的重要位:plcChild1plcChild2

<!-- markup, controls, etc -->
<div class="shell">
    <!-- etc -->

    <!-- optional content with default, will map to `ChildContentOne` -->
    <asp:PlaceHolder ID="plcChild1" runat="server">
        Some default content in the first child.
        Will show this unless overwritten.
        Include HTML, controls, whatever.
    </asp:PlaceHolder>

    <!-- etc -->

    <!-- optional content, no default, will map to `ChildContentTwo` -->
    <asp:PlaceHolder ID="plcChild2" runat="server"></asp:PlaceHolder>

</div>

后面的局部/用户控制代码:qazxsw POI

mycontrol.ascx.cs

重要位(?):

  • [ParseChildren(true), PersistChildren(true)] [ToolboxData(false /* don't care about drag-n-drop */)] public partial class MyControlWithNestedContent: System.Web.UI.UserControl, INamingContainer { // expose properties as attributes, etc /// <summary> /// "attach" template to child controls /// </summary> /// <param name="template">the exposed markup "property"</param> /// <param name="control">the actual rendered control</param> protected virtual void attachContent(ITemplate template, Control control) { if(null != template) template.InstantiateIn(control); } [PersistenceMode(PersistenceMode.InnerProperty), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] public virtual ITemplate ChildContentOne { get; set; } [PersistenceMode(PersistenceMode.InnerProperty), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] public virtual ITemplate ChildContentTwo { get; set; } protected override void CreateChildControls() { // clear stuff, other setup, etc // needed? base.CreateChildControls(); this.EnsureChildControls(); // cuz...we want them? // using the templates, set up the appropriate child controls attachContent(this.ChildContentOne, this.plcChild1); attachContent(this.ChildContentTwo, this.plcChild2); } } - 这样的东西ParseChildren
  • shows up - 所以动态创建的东西不会被重置?
  • PersistChildren - 所以控制是PersistenceMode(PersistenceMode.InnerProperty)
  • parsed correctly - 同上?

控制使用

DesignerSerializationVisibility(DesignerSerializationVisibility.Content)

1
投票

我的猜测是,你正在寻找这样的事情? <%@ Register Src="~/App_Controls/MyStuff/mycontrol.ascx" TagPrefix="me" TagName="MyNestedControl" %> <me:MyNestedControl SomeProperty="foo" SomethingElse="bar" runat="server" ID="meWhatever"> <%-- omit `ChildContentOne` to use default --%> <ChildContentTwo>Stuff at the bottom! (not empty anymore)</ChildContentTwo> </me:MyNestedControl>

你的标签被拆除或不可见,所以不能真正帮助你。

© www.soinside.com 2019 - 2024. All rights reserved.