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 派生的自定义用户控件类.
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 用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将子节点添加到从 System.Web.UI.Control 派生的自定义 asp.net 用户控件
基础教程推荐
- c# Math.Sqrt 实现 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何激活MC67中的红灯 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01