自定义控件开发2--控件开发
首先看段代码
[PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty),
NotifyParentProperty(true)]
public string Text
{
get { return _text; }
set { _text = value; }
}
[NotifyParentProperty(true)]
public string Value
{
get { return _value; }
set { _value = value; }
}
NotifyParentProperty(true)]
public string Text
{
get { return _text; }
set { _text = value; }
}
[NotifyParentProperty(true)]
public string Value
{
get { return _value; }
set { _value = value; }
}
会发现Value属性,并没有标志PersistenceMode属性,那么它的默认属性就是PersistenceMode.Attribute. 由于我们设置了Text为默认属性,所以当我们将Value也设置为InnerDefaultProperty的时候,Value的值没被呈现。原因是我们:
[ParseChildren(true,"Text")]
public class MyItem
public class MyItem
标记了MyItem类为默认属性.
[ParseChildren(true, "Items")]
[ControlBuilder(typeof(ScriptItemBuilder))]
public class MyItemsControl : WebControl, INamingContainer
{
private MyItems _items;
[PersistenceMode(PersistenceMode.InnerDefaultProperty),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content)
]
public MyItems Items
{
get
{
if (_items == null)
{
_items = new MyItems();
} return _items;
}
}
[ControlBuilder(typeof(ScriptItemBuilder))]
public class MyItemsControl : WebControl, INamingContainer
{
private MyItems _items;
[PersistenceMode(PersistenceMode.InnerDefaultProperty),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content)
]
public MyItems Items
{
get
{
if (_items == null)
{
_items = new MyItems();
} return _items;
}
}
这里我自定义了ControlBuilder来编辑Items集合。它可以解析并返回任意的类型,而我们前面提到的AddParsedSubObject只能解析标记有runat='Server'的控件。
而ControlBuilder可以解析任意标签。
public class MyItemerBuilder : ControlBuilder
{
public override Type GetChildControlType(string tagName, IDictionary attribs)
{
if (string.Compare(tagName.ToLower(), "MyItem", false, CultureInfo.InstalledUICulture) == 0)
{
return typeof(MyItem);
}
return null;
}
}
{
public override Type GetChildControlType(string tagName, IDictionary attribs)
{
if (string.Compare(tagName.ToLower(), "MyItem", false, CultureInfo.InstalledUICulture) == 0)
{
return typeof(MyItem);
}
return null;
}
}
浙公网安备 33010602011771号