积极答复者
为什么自定义Asp.Net控件包含一个集合属性时会在设计时出错?运行时没有问题?

问题
-
aspx代码如下,为什么VS2008在设计时下会出现错误:
创建控件时出错
未能对属性"Items"设置"cjl:Test"<%@ Register Assembly="MySite.Controls" Namespace="MySite.Controls" TagPrefix="cjl" %> <cjl:Test ID="Pager1" runat="server"> <Items> <cjl:Item Text="33" /> </Items> </cjl:Test>
using System.Collections.Generic; using System.ComponentModel; using System.Web.UI; namespace MySite.Controls { [ToolboxData("<{0}:test runat=\"server\"></{0}:test>")] [ParseChildren(true)] [PersistChildren(false)] public class Test : Control { [PersistenceMode(PersistenceMode.InnerProperty)] [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Content)] [NotifyParentProperty(true)] public ItemCollection Items { get { object obj = ViewState["Items"]; if (obj == null) { return null; } return obj as ItemCollection; } set { EnsureChildControls(); ViewState["Items"] = value; } } } public class ItemCollection : List<Item> { } public class Item { public string Text { get; set; } } }
答案
-
参考 http://officenotes.spaces.live.com/blog/cns!43AE5D331F584382!328.entry
终于找到了解决方法
集合属性Items不要Set方法,将其注释后就可以在设计时正确显示了。
get
{
object obj = ViewState["Items"];
if (obj == null)
{
return return ew ItemCollection();
}
return obj as ItemCollection;
}
/*set
{
EnsureChildControls();
ViewState["Items"] = value;
}*/
- 已标记为答案 阿良.NET 2010年5月23日 0:04
全部回复
-
[ToolboxData("<{0}:test runat=\"server\"></{0}:test>")]
[ParseChildren(true)]
[PersistChildren(false)]
public class Test : Control
{
[PersistenceMode(PersistenceMode.InnerProperty)]
[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Content)]
[TypeConverter(typeof( CollectionConverter))][NotifyParentProperty(true)]
public ItemCollection Items
{
get
{
object obj = ViewState["Items"];
if (obj == null)
{
return null;
}
return obj as ItemCollection;
}
set
{
EnsureChildControls();
ViewState["Items"] = value;
}
}
public class ItemCollection : List<Item>{
}
public class Item{
public string Text { get; set; }}
}
77138191qq群 .net与asp.net -
参考 http://officenotes.spaces.live.com/blog/cns!43AE5D331F584382!328.entry
终于找到了解决方法
集合属性Items不要Set方法,将其注释后就可以在设计时正确显示了。
get
{
object obj = ViewState["Items"];
if (obj == null)
{
return return ew ItemCollection();
}
return obj as ItemCollection;
}
/*set
{
EnsureChildControls();
ViewState["Items"] = value;
}*/
- 已标记为答案 阿良.NET 2010年5月23日 0:04