无法从''属性的字符串表示'(Collection)'创建'System.Collections.Generic.List'类型的对象

时间:2011-11-09 16:49:56

标签: 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")] 
  [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); 

  } 
 } 

首先,当我将此控件拖放到页面上时,我可以在设计时从属性窗口访问Items属性。

这是它的aspx代码:

<Aram:News ID="News1" runat="server" /> 

修改项目后(显示值为集合的值)我看不到设计视图的任何更新。

点击确定后,开始调试,我收到错误:

Cannot create an object of type 'System.Collections.Generic.List`1[[AramWebControls.NewsItem, Custom Web Server Control, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' from its string representation '(Collection)' for the 'Items' property.

现在我回顾一下我的控件的aspx代码,它的Items属性设置为'(Collection)'。

<Aram:News ID="News1" runat="server" Items="(Collection)" /> 

如何解决这个问题?我在说我的控件应该将List控件作为内容。

任何帮助将不胜感激。

感谢。

2 个答案:

答案 0 :(得分:0)

我找到了解决这个问题的方法。 首先,Items是List类型,是一个集合。它不能在News控件标记内持久化。所以它必须是新闻控制标签内容的一部分。这需要自定义视图状态管理。

有关详细信息,您可以先参考Web Control Collection Property Example 然后,您需要覆盖LoadViewState,SaveViewState和TrackViewState来处理项目。有关此内容的更多信息,请参阅Server Control Properties Example

答案 1 :(得分:0)

我的ASP.net项目的Designer中出现了类似的错误。代码构建并运行正常,但Designer显示以下错误:

public class ConnectionActivity extends AppCompatActivity {

    ConnectionClass connectionClass;
    EditText edtuserid,edtpass;
    Button btnlogin;
    ProgressBar pbbar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_connection);

        connectionClass = new ConnectionClass();
        edtuserid = (EditText) findViewById(R.id.edtuserid);
        edtpass = (EditText) findViewById(R.id.edtpass);
        btnlogin = (Button) findViewById(R.id.btnlogin);
        pbbar = (ProgressBar) findViewById(R.id.pbbar);
        pbbar.setVisibility(View.GONE);

        btnlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DoLogin doLogin = new DoLogin();
                doLogin.execute("");

            }
        });

    }

我之前在服务器控件代码中的相关属性上有此属性:

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。