Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将子节点添加到从System.Web.UI.control派生的自定义asp.net用户控件_C#_.net_Asp.net_User Controls - Fatal编程技术网

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

C# 如何将子节点添加到从System.Web.UI.control派生的自定义asp.net用户控件,c#,.net,asp.net,user-controls,C#,.net,Asp.net,User Controls,我想知道如何将一些额外的子节点添加到从System.Web.UI.control派生的自定义用户控件类中 例如,当前我有一个控件,它不包含子节点,并且在设计图面上看起来如下所示 <cust:MyCustomControl id="ctlMyCustomControl" runat="server" attribute1="somevalue" attribute2="somevalue" ></MyCustomControl> 我想要的是能够从设计图面向该控件添加n

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

例如,当前我有一个控件,它不包含子节点,并且在设计图面上看起来如下所示

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

我想要的是能够从设计图面向该控件添加n个子节点,然后从代码中访问它们的值。因此,添加到上述控件中

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

价值1
价值2
我不清楚如何访问子节点

任何关于如何做到这一点的见解都将受到赞赏。

您希望能够做到这一点

要能够具有以下标记:

<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; }
}
[ParseChildren(true)]
[儿童(对)]
[ToolboxData(“”)
公共类CustomControlUno:WebControl,INamingContainer
{
私人控制1儿童收藏(U儿童);;
[PersistenceMode(PersistenceMode.InnerProperty)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
公共控制1儿童收集儿童
{
得到
{
if(_children==null)
_children=新控件1childrencollection();
返回儿童;
}
}
}
公共类控件1儿童集合:列表
{
}
公共班级控制1儿童
{
公共整数整型属性{get;set;}
私有字符串StringProperty{get;set;}
}

如果希望支持给定的语法(无需使用标记前缀),可以使用ControlBuilder:

 //MyControlBuilder 
 public class MyControlBuilder : ControlBuilder
 {
   public override Type GetChildControlType(string tagName, IDictionary attribs)
   { 
     if (tagName.StartsWith("childnode")) return typeof(Control);
     else return base.GetChildControlType(tagName,attribs);
   }

   public override void AppendLiteralString(string s)
   { 
     //ignore literals betwen tags
   }
 }

 //MyCustomControl
 [ParseChildren(false)]
 [ControlBuilder(typeof(MyControlBuilder))]
 public class MyCustomControl : Control
 {
   public string attribute1 {get;set;}
   public string attribute2 {get;set;}

   protected override void AddParsedSubObject(object obj)
   {
     Control ctrl = (Control) obj;
     LiteralControl childNode = ctrl.Controls[0];

     //Add as-is (e.g., literal "value1")
     Controls.Add(childNode);
   }
 }

另请参见

我给出了一个答案,这个答案几乎就是您所追求的标记-如果您必须按照所显示的格式进行标记,请告诉我。我认为它是可以做到的,并且会有一个根,让你看看我是否在某处找到了答案=)@Rob,这正是我想要的,实际上额外的父节点“Children”更好,因为你可以更明确地了解Children节点代表什么。非常感谢你!!没问题,很高兴我能帮助=)(我同意你的观点:额外的父节点)