自定义控件的子控件从WebControl中继承,但在每次添加子控件时,却会在CS文件中自动生成保护类型的变量定义。如:protected MyControlItem controlItem1;
要达到的效果类似IE Webcontrol中的TreeView的子控件Nodes的效果,无论添加了多少个node,都只会生成一个TreeView的变量定义,不会把每个node都生成一个定义。

解决方案 »

  1.   

    If I understand you correctly, try to derive from Control or try to add[PersistChildren(true)]to your control?
      

  2.   

    多谢思归.
    试了一下:[PersistChildren(true)],发觉不行呀,如果此特性为true的话,则会导致我的子控件无法在ASPX页面中自动进行序列化保持,即自动生成子控件标记<waki:item Text="text1"></waki:item>.
    现在发现奇怪的现象是:使用系统的集合编辑器往我的子控件集合中添加完子控件后,在CS页中,会生成保护类型变量,即:Protected WakiItem wakiItem1;
    如果返回ASPX页面中查看一下HTML代码,再看一下设计样式,再返回CS页面时,发现刚才的保护类型变量申明已被删除.如果不去查看直接执行的话,则变量申明还存在.怎样让它一开始就不自动生成呢?
      

  3.   

    what kind of child controls? are those part of a collection control? if so, seehttp://www.west-wind.com/weblog/posts/200.aspx
      

  4.   

    子控件是一个控件集合。但http://www.west-wind.com/weblog/posts/200.aspx没有解决问题。
    我看他的控件跟我的效果是一样的。在IDE中添加一个TabPage时,在CS文件中会生成一个变量定义。
    如果浏览ASPX的HTML视图的话,再看CS文件时,会发现变量定义已被删除!
      

  5.   

    ok, sorry, I misunderstood, how did you implement your collection class? is it derived from ControlCollectin? can you look into how Table/TableRow are implemented in Reflector?
      

  6.   

    Here are my findings, you need to create a CollectionEditor like this public class TabCollectionEditor : CollectionEditor
    { public TabCollectionEditor(Type type) : base(type){}

    protected override object CreateInstance(Type itemType) 
    {
    return Activator.CreateInstance(itemType, BindingFlags.CreateInstance | (BindingFlags.Public | BindingFlags.Instance), null, null, null);
    }

    }then add the following attribute to your collection class:[Editor(typeof(TabCollectionEditor),typeof(UITypeEditor))]
    public class TabPageCollection : CollectionBase 
    {
      //your collection class here
    }
      

  7.   

    多谢思归,问题解决了。但又带来了一个新问题,我的集合子控件本身使用了一个Design,主要目的是使在子控件被创建时,自动设置默认属性值:Value,但在加了集合Design之后,子控件的Design不起作用了。子控件的Design与集合类的Design之间有何联系,要怎样才能避免冲突呢?
    design代码如下:
    public class BarItemDesign:System.Web.UI.Design.ControlDesigner
    {
    public BarItemDesign()
    {
    }
    public static Int32 itemNum = 0; public override void OnSetComponentDefaults()
    {
    CoolBarItem barItem = (CoolBarItem)this.Component;
    base.OnSetComponentDefaults ();
    barItem.Value = "barItem" + (++itemNum).ToString();
    }
    }
    单独写时是起作用的。
      

  8.   

    因vs.net2005还没出正式版,还未在它底下测试过。不知情况怎样。