Asp.net 无法创建类型为';System.Collections.Generic.List'';从它的字符串表示形式';(收集)和#x27;对于'';所有物
我在asp.net中创建了一个自定义web服务器控件。我试图在RenderContents方法中呈现一个类的列表 这是我的密码:Asp.net 无法创建类型为';System.Collections.Generic.List'';从它的字符串表示形式';(收集)和#x27;对于'';所有物,asp.net,.net,custom-server-controls,Asp.net,.net,Custom Server Controls,我在asp.net中创建了一个自定义web服务器控件。我试图在RenderContents方法中呈现一个类的列表 这是我的密码: [DefaultProperty("Items")] [ToolboxData("<{0}:News runat=server></{0}:News>")] public class News : WebControl { [Bindable(true)] [Category("Appearance")] [D
[DefaultProperty("Items")]
[ToolboxData("<{0}:News runat=server></{0}:News>")]
public class News : WebControl
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public List<NewsItem> Items
{
get
{
List<NewsItem> items = (List<NewsItem>)(ViewState["Items"] == null ? new List<NewsItem>(): ViewState["Items"]);
return items;
}
set
{
ViewState["Items"] = value;
}
}
protected override void RenderContents(HtmlTextWriter output)
{
foreach (var item in Items)
item.RenderControl(output);
}
}
现在我回顾一下控件的aspx代码,它的Items属性设置为“(Collection)”
如何解决这个问题?我想我的控件应该将列表控件作为内容
任何帮助都将不胜感激
谢谢。我找到了这个问题的解决方案。 首先,项的类型是List,这是一个集合。无法将其保留在新闻控件标记内。所以它必须是新闻控制标签内容的一部分。这需要自定义viewstate管理 有关详细信息,请首先参阅
然后需要覆盖LoadViewState、SaveViewState和TrackViewState来处理这些项。有关此项目的更多信息,请参阅我在ASP.net项目的设计器中遇到类似错误。代码生成并运行正常,但设计器显示以下错误:
Error Creating Control - [ServerControlNameOmitted] Cannot create an object of type 'System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' from its string representation '(Collection)' for the 'CustomProperties' property.
我以前在服务器控制代码的相关属性上具有此属性:
[Browsable(false)]
public Dictionary<string, object> customProperties { get; set; }
[可浏览(错误)]
公共字典customProperties{get;set;}
要修复它,我将其更改为:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public Dictionary<string, object> customProperties { get; set; }
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[可浏览(false)、可编辑可浏览(editorbrowseblestate.Never)]
公共字典customProperties{get;set;}
请注意,我没有尝试在回发之间保留customProperties。这是什么意思
Items=“(Collection)”
?“Collection”字符串显式设置,或者这里有一些数据绑定到属性?@sll我不接触aspx代码。我只是从“属性”窗口更新了“设计时”属性,并添加了几个项。当我完成时,它只是以这种方式显示出来。当您在属性窗口中单击项目
属性时,您不会看到添加的项目。它又空了。您以前是否设计过自定义web控件?
Error Creating Control - [ServerControlNameOmitted] Cannot create an object of type 'System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' from its string representation '(Collection)' for the 'CustomProperties' property.
[Browsable(false)]
public Dictionary<string, object> customProperties { get; set; }
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public Dictionary<string, object> customProperties { get; set; }