如何将子节点添加到从 System.Web.UI.Control 派生的自定义 asp.net 用户控件

How to add child nodes to custom asp.net user control derived from System.Web.UI.Control(如何将子节点添加到从 System.Web.UI.Control 派生的自定义 asp.net 用户控件)
本文介绍了如何将子节点添加到从 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 用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)