c# – 自定义用户控件中的ASP嵌套标记

前端之家收集整理的这篇文章主要介绍了c# – 自定义用户控件中的ASP嵌套标记前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我刚刚开始使用c#中的自定义用户控件,我想知道是否有任何关于如何编写接受嵌套标签的示例.例如,当您创建“asp:repeater”时,可以为“itemtemplate”添加嵌套标记.

有任何帮助!

干杯

解决方法

不久前我写了一篇关于这个的 blog post.简而言之,如果您有一个带有以下标记的控件:
  1. <Abc:CustomControlUno runat="server" ID="Control1">
  2. <Children>
  3. <Abc:Control1Child IntegerProperty="1" />
  4. </Children>
  5. </Abc:CustomControlUno>

您需要控件中的代码遵循以下方式:

  1. [ParseChildren(true)]
  2. [PersistChildren(true)]
  3. [ToolBoxData("<{0}:CustomControlUno runat=server></{0}:CustomControlUno>")]
  4. public class CustomControlUno : WebControl,INamingContainer
  5. {
  6. private Control1ChildrenCollection _children;
  7.  
  8. [PersistenceMode(PersistenceMode.InnerProperty)]
  9. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  10. public Control1ChildrenCollection Children
  11. {
  12. get
  13. {
  14. if (_children == null)
  15. {
  16. _children = new Control1ChildrenCollection();
  17. }
  18. return _children;
  19. }
  20. }
  21. }
  22.  
  23. public class Control1ChildrenCollection : List<Control1Child>
  24. {
  25. }
  26.  
  27. public class Control1Child
  28. {
  29. public int IntegerProperty { get; set; }
  30. }

猜你在找的C#相关文章