ASP.NET的强制转换异常无效

本文关键字:转换 异常 无效 NET ASP | 更新日期: 2023-09-27 18:21:29

以下是总体代码:

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还很陌生,所以我不知道是什么原因导致了它,欢迎修复!

ASP.NET的强制转换异常无效

我想这是因为你在做

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>();

您有两个地方可以设置"books":CCD_ 3,其中CCD_。

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

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