ASP.NET3.5控件和组件开发:ParseChildrenAttribute与PersistChildrenAttribute小结

    技术2022-05-20  32

    对于它们的理解,纠结了不少时间.

    1)ParseChildrenAttribute类:

          1.定义:标记服务器控件将指示分析器把包含在服务器控件标记内的元素解释为属性.

          2.默认false,定义在类的头部,通常与PersistChildrenAttribute一起使用 .

          通俗理解,在定义自定义服务器控件时,某属性为复杂属性的情况下,通过对它设置,使该属性在设计器中,显示的方式是否为属性.

          3.常用构造函数有两种,(Boolean)和(Boolean, String),第一个参数都一样,是否显示为控件的属性;第二个参数定义了一个服务控件的集合属性,常在内部嵌套默认复杂属性时用到 .

    [ParseChildren(true, "Items")] public class CollectionControlProperty : WebControl { private Items items; [PersistenceMode(PersistenceMode.InnerDefaultProperty)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] [NotifyParentProperty(true)] [TypeConverter(typeof(CollectionConverter))] [Category("复杂属性")] [Description("复杂属性——内部默认嵌套形式")] public Items Items { get { if (this.items == null) { this.items = new Items(); } return this.items; } } … … }

     

     

    2)PersistChildrenAttribute类

        1.定义:是否将嵌套内容作为嵌套控件保持.

        2.默认为true

        通俗理解,在定义自定义服务器控件时,某属性为复杂属性的情况下,在是否对类持久化,在自定义控件内容中,作为控件显示.

     

    3) 继承WebControl类

       1.它们分别有各自的默认值,在开发控件时,如果继承WebControl类,WebControl会重新设置它们的值,[ParseChildren(true)],[PersistChildren(false)], 正好它们的默认值相反,若开发时,继承Control类,则需要注意.

     

    4) 它们的结合使用

        如果 PersistChildrenAttributetrueParseChildrenAttributefalse ,则 ASP.NET 服务器控件包含的嵌套内容将作为控件保持。 如果 PersistChildrenAttributefalseParseChildrenAttributetrue ,则嵌套内容将作为服务器控件的属性保持。   

     

       注:详细参照MSDN,写的有误的地方,请指正.


    最新回复(0)