自定义控件开发---集合控件(类似DropDownList,ListBox)

我定义了多个默认属性(MyItem,MyAnotherItem).并且希望在设计期 运行时都正常,我们必须做2件事情.

1. 将MyItem,MyAnotherItem继承Control.

2. 重写AddParsedSubObject方法

    protected override void AddParsedSubObject(object obj)
        {
            
if (obj is MyItem)
            {
                
if (this._items == null)
                {
                    
this._items = new MyItems();
                } 
this._items.Add((MyItem)obj);
            } 
if (obj is MyAnotherItem)
            {
                
if (this._anotherItem == null)
                {
                    
this._anotherItem = new MyAnotherItems();
                } 
this._anotherItem.Add((MyAnotherItem)obj);
            }
        }

 3. 标记当前控件以控件方式解析属性。

    [ParseChildren(false)]
    
public class MyItemsControl : WebControl, INamingContainer

DownLoad

posted on 2009-11-16 16:29  博览潇湘  阅读(435)  评论(0)    收藏  举报

导航