无效的Cast异常ASP.NET

时间:2012-01-27 01:56:17

标签: c# asp.net .net

下面是整体代码:

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很陌生,所以我迷失了可能导致它的原因,修复了欢迎!

3 个答案:

答案 0 :(得分:2)

我想这是因为你在做

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

然后您尝试将List<string>投射到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];

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

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

答案 1 :(得分:0)

您有2个地方可以设置“图书”: ViewState["books"] = books;其中books = new List<String>();

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

答案 2 :(得分:0)

ViewState["books"]List<string>,而不是string[]。删除(string [])它应该工作。实际上,您也可以删除新的List()。