【问题标题】:Invalid Cast Exception ASP.NET无效的强制转换异常 ASP.NET
【发布时间】:2012-02-20 03:13:21
【问题描述】:

以下是整体代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class partin : System.Web.UI.Page
{
private List<String> books = new List<String>();

void Page_PreRender()
{
    Item_Listbox.DataSource = books;
    Item_Listbox.DataBind();   
}

int SortASC(string x, string y)
{
    return String.Compare(x, y);
}

int SortDESC(string x, string y)
{
    return String.Compare(x, y) * -1;
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {

        Header_Label.Text = "Welcome! Please select a book category.";
        Item_Listbox.DataSource = books;
        Item_Listbox.DataBind();

    }

}

protected void Fiction_Click(object sender, EventArgs e)
{

        Header_Label.Text = "Fiction Section";

        books.Add("Title: The Old Man and The Sea | Decription: An epic novel. | Price: 10 USD | Quantity: 3");
        books.Add("Title: A Game of Thrones | Decription: A tale of fire and ice. | Price: 15 USD | Quantity: 6");
        books.Add("Title: Dracula | Decription: A book about vampires. | Price: 5 USD | Quantity: 7");
        books.Add("Title: Twilight | Decription: An awful book. | Price: Free | Quantity: 1000");

        Item_Listbox.DataSource = books;
        Item_Listbox.DataBind();

        ViewState["books"] = books;

}


protected void Non_Fiction_Click(object sender, EventArgs e)
{
    Header_Label.Text = "Non-Fiction Section";



}
protected void Self_Help_Click(object sender, EventArgs e)
{
    Header_Label.Text = "Self Help Section";



}

protected void Sort_Command(object sender, CommandEventArgs e)
{
    if (e.CommandName == "Sort")
    {
        switch (e.CommandArgument.ToString())
        {
            case "ASC":
                books.Sort(SortASC);
                break;
            case "DESC":
                books.Sort(SortDESC);
                break;
        }

    }
    if (ViewState["books"] == null)
        ViewState["books"] = new string[0];

    Item_Listbox.DataSource = new List<string>((string[])ViewState["books"]);
    Item_Listbox.DataBind();  
}



}

这里抛出了无效的强制转换异常:

Item_Listbox.DataSource = new List<string>((string[])ViewState["books"]);

我对 ASP.NET 很陌生,所以我不知道是什么原因造成的,欢迎修复!

【问题讨论】:

  • 那行应该做什么?你不想要Item_Listbox.DataSource = ViewState["books"]吗?

标签: c# asp.net .net


【解决方案1】:

我猜是因为你在做

private List<String> books = new List<String>();
//...
ViewState["books"] = books;

然后您尝试将List&lt;string&gt; 转换为string[]

Item_Listbox.DataSource = new List<string>((string[])ViewState["books"]);

用以下方式重写最后一行应该可以解决问题:

Item_Listbox.DataSource = (List<string>)ViewState["books"];

甚至

Item_Listbox.DataSource = ViewState["books"];

第二个你有以下代码的地方:

if (ViewState["books"] == null)
        ViewState["books"] = new string[0];

If 语句的结果为 false,因为在单击按钮后已经设置了 ViewState,但总的来说,我建议与您正在使用的数据结构保持一致,并将此代码更改为以下内容:

if (ViewState["books"] == null)
        ViewState["books"] = new List<string>();

【讨论】:

  • 不需要演员表。它不适用于ViewState["books"] 设置为new string[0] 的情况。
  • 好的,有了这个更正,我仍然遇到我在另一个线程中发布的问题,当我点击按钮执行 ASC 或 DESC(单击小说按钮并填充列表框后)它会然后清除列表框,而不是按 ASC 或 DESC 顺序放置。感谢你目前的帮助!对此错误有何建议?
【解决方案2】:

ViewState["books"] 是 List&lt;string&gt;,而不是 string[]。删除 (string[]) 它应该可以工作。事实上,你也可以删除 new List( )。

【讨论】:

    【解决方案3】:

    您有 2 个地方设置“书籍”: ViewState["books"] = books; 其中books = new List&lt;String&gt;();。

    ViewState["books"] = new string[0];

    【讨论】:

      猜你喜欢
      • 2012-09-21
      • 1970-01-01
      • 1970-01-01
      • 2012-05-28
      • 2015-01-27
      • 2012-09-20
      • 2013-11-06
      • 1970-01-01
      • 2021-11-14
      相关资源
      最近更新 更多