我刚刚开始使用c#中的自定义用户控件,我想知道是否有任何关于如何编写接受嵌套标签的示例.例如,当您创建“asp:repeater”时,可以为“itemtemplate”添加嵌套标记.
有任何帮助!
干杯
解决方法
不久前我写了一篇关于这个的
blog post.简而言之,如果您有一个带有以下标记的控件:
- <Abc:CustomControlUno runat="server" ID="Control1">
- <Children>
- <Abc:Control1Child IntegerProperty="1" />
- </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; }
- }