如何将子节点添加到从 System.Web.UI.Control 派生的自定义 asp.net 用户控件 [英] How to add child nodes to custom asp.net user control derived from System.Web.UI.Control

查看:11
本文介绍了如何将子节点添加到从 System.Web.UI.Control 派生的自定义 asp.net 用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将一些额外的子节点添加到从 System.Web.UI.Control 派生的自定义用户控件类.

I would like to know how to add some additional child nodes to a custom user control class derived from System.Web.UI.Control.

例如,目前我有一个不包含子节点的控件,并且在设计图面上如下所示.

For example currently I have a control that contains no child nodes and on the design surface looks like the following.

<cust:MyCustomControl id="ctlMyCustomControl" runat="server" attribute1="somevalue" attribute2="somevalue" ></MyCustomControl>

我正在寻找的是能够从设计表面向该控件添加 n 个子节点,然后从代码中访问它们的值.因此添加到上述控件中.

What I am looking for is to have the ability to add n number of child nodes to this control from the design surface and then access their values from the code. So adding to the control stated above.

<cust:MyCustomControl id="ctlMyCustomControl" runat="server" attribute1="somevalue" attribute2="somevalue" >
  <childnode1>value1</childnode1>
  <childnode2>value2</childnode2>
</MyCustomControl>

我不清楚如何访问子节点.

It is not clear to me how to access the child nodes.

感谢任何有关如何做到这一点的见解.

Any insight on how to do this is appreciated.

推荐答案

你希望能够 以声明方式描述 asp.net 控件属性.

为了能够有以下标记:

<Abc:CustomControlUno runat="server" ID="Control1">
    <Children>
        <Abc:Control1Child IntegerProperty="1" StringProperty="Item1" />
        <Abc:Control1Child IntegerProperty="2" StringProperty="Item2" />
    </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; }
    private string StringProperty { get; set; }
}

这篇关于如何将子节点添加到从 System.Web.UI.Control 派生的自定义 asp.net 用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆