集合:
public class DataItem:System.Web.UI.Control , IStateManager
{...}
public class DataItemCollection : CollectionBase, IStateManager
{...}
自定義控件:
[Description(""),
Designer("MyWebControls.ComboBoxDesigner"), 
ToolboxData("<{0}:ComboBox runat=server></{0}:ComboBox>")]
public class ComboBox : WebControl, INamingContainer ,IStateManager
{
private DataItemCollection m_items;
...
//集合属性
[DefaultValue(null),
PersistenceMode(PersistenceMode.InnerProperty),
NotifyParentProperty(true),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
Editor(typeof(CollectionEditor), typeof(UITypeEditor))]
public DataItemCollection Items
{
get
{
if(m_items == null)
 m_items = new DataItemCollection();
return this.m_items;
}
}
...
}
最后在我的页面中通过属性编辑器编辑后声称代码如下:
<cc1:ComboBox id="ComboBox6" runat="server">
<Items>
<cc1:DataItem Value="1" Text="test1" ID="dataItem1"></cc1:DataItem>
<cc1:DataItem Value="2" Text="test2" ID="dataItem2"></cc1:DataItem>
</Items>
</cc1:ComboBox>看似OK了,但是在运行的时候,出现对<Items>无法识别的错误,辗转无法解决,只好请教高手们帮我看看我的做法有什么不妥...

解决方案 »

  1.   

    你这个是自定义的模版吧???就是像datalist的itemtemplate???
    在.net里,,如果想要自定义模版,,需要用一个叫ITemplate的接口,,private ITemplate _itemTemplate;
    [TemplateContainer(typeof(CurrenCyInformation))]
    {
    get{return _itemTemplate;}
    set{_itemTemplate = value;}
    }
    不知道你明不明白,,你能买到asp.net pro 2002--2003中文合集吗?那个上面有介绍的
      

  2.   

    应该是这样用吧:<cc1:ComboBox id="ComboBox6" runat="server">
    <cc1:DataItem Value="1" Text="test1" ID="dataItem1"></cc1:DataItem>
    <cc1:DataItem Value="2" Text="test2" ID="dataItem2"></cc1:DataItem>
    </cc1:ComboBox>可以去www.metabuilds.com 然后下载一个DefaultButtons 或ComboBox控件的源代码研究一下
      

  3.   

    回复人: webdiyer(陕北吴旗娃) ( ) 信誉:126 ???信誉值怎么会126呢
      

  4.   

    看看偶在http://community.csdn.net/Expert/topic/2968/2968427.xml?temp=.9858972的回答可能对你有帮助:)
      

  5.   

    try use something like....
    [ParseChildren(true)] 
    public class ComboBoxseehttp://west-wind.com/weblog/posts/200.aspx
      

  6.   

    saucer(思归) 的提議我嘗試過了,還是老樣子,無法識別<Items>,對於你提供的文章中,它同樣生成了<tabpages>的標記,何以他能夠正常運行?
    對於 micropentium6(小笨|曾经的美好) 兄的帖子我還得鑽研一下,初步看了,沒有抓到要領
      

  7.   

    [ParseChildren(true, "Items")]
    public class ComboBox
      

  8.   

    lhcoolhacker(木子园):
    你的做法會讓我在添加這個自定義控件的時候(還未添加集合屬性內容),出現Creating Error
    如何是好
      

  9.   

    我下載了一個例子,仔仔細細的比較了一下,終於找到導致這個問題的根源了
    說來好笑,其實我貼出來的代碼都沒有問題的,無怪大家幫不了我
    產生錯誤的原因是:在DataItemCollection類中我重載了this屬性,如下:
    public DataItem this[int index]
    {
    get
    {
    return (DataItem)List[index];
    }
    set
    {
    List[index] = value;
    }
    }
    public DataItem this[string ItemValue]
    {
    get
    {
    foreach(DataItem di in this)
    if(di.Value == ItemValue)
    return di;
    return null;
    }
    }
    .....
    現在細細想來,原因可能是這樣的經過虛列化後,this後的參數都變成了string類型,因此,程序直接從xml中讀取該集合的時候,會出現無法解析的錯誤.好了,謝謝各位盡心的幫忙,因為例子主要是從'saucer(思归) '提供的網站上下載的,所以這裡就主要把分給他了謝謝.