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

查看:148
本文介绍了如何子节点添加到从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个从设计图面添加到这种控制的能力,然后从code访问它们的值。因此,增加了上述的控制。

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.

这是如何做到这一点的任何观点是AP preciated。

Any insight on how to do this is appreciated.

推荐答案

您希望能够<一个href="http://www.robertwray.co.uk/blog/2008/02/describing-aspnet-control-properties-declaratively.html">describe asp.net控件属性声明的。

要能够有如下标记:

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

您需要以下code:

[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天全站免登陆