【问题标题】:How to add child nodes to custom asp.net user control derived from System.Web.UI.Control如何将子节点添加到从 System.Web.UI.Control 派生的自定义 asp.net 用户控件
【发布时间】:2011-04-08 05:59:57
【问题描述】:

我想知道如何将一些额外的子节点添加到从 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>

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

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

【问题讨论】:

  • 我给出的答案几乎是你在标记之后所追求的 - 如果你必须有格式中的标记,请告诉我你已经展示了。我认为这是可行的,并且会在你身边找到一个根,看看我是否在某个地方找到了答案 =)
  • @Rob,这正是我想要的,实际上额外的父节点“子节点”更好,因为您可以更明确地了解子节点代表的内容。非常感谢!!
  • 没问题,很高兴我能帮上忙 =) (我同意你的观点:额外的父节点)

标签: c# .net asp.net user-controls


【解决方案1】:

你希望能够describe asp.net control properties declaratively

为了能够有以下标记:

<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; }
}

【讨论】:

    【解决方案2】:

    如果您想按原样支持给定的语法(无需使用标签前缀),您可以使用 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);
       }
     }
    

    另见http://msdn.microsoft.com/en-us/library/system.web.ui.controlbuilder.aspx

    【讨论】:

      猜你喜欢
      • 2013-07-16
      • 2011-01-28
      • 2012-02-19
      • 1970-01-01
      • 2011-11-10
      • 1970-01-01
      • 2011-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多