Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无效的强制转换异常ASP.NET_C#_Asp.net_.net - Fatal编程技术网

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

C# 无效的强制转换异常ASP.NET,c#,asp.net,.net,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<Str

以下是总体代码:

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



}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
公共部分类partin:System.Web.UI.Page
{
私有列表图书=新列表();
无效页面_PreRender()
{
Item_Listbox.DataSource=书籍;
Item_Listbox.DataBind();
}
int-SortASC(字符串x,字符串y)
{
返回字符串。比较(x,y);
}
int-SortDESC(字符串x,字符串y)
{
返回字符串。比较(x,y)*-1;
}
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
Header\u Label.Text=“欢迎!请选择图书类别。”;
Item_Listbox.DataSource=书籍;
Item_Listbox.DataBind();
}
}
受保护的无效链接单击(对象发送者,事件参数e)
{
Header_Label.Text=“虚构部分”;
书籍。添加(“标题:老人与海|描述:史诗小说|价格:10美元|数量:3”);
书籍。添加(“标题:权力游戏|描述:火与冰的故事。|价格:15美元|数量:6”);
书籍。添加(“标题:德古拉|描述:一本关于吸血鬼的书。|价格:5美元|数量:7”);
书籍。添加(“标题:暮光之城|描述:一本糟糕的书。|价格:免费|数量:1000”);
Item_Listbox.DataSource=书籍;
Item_Listbox.DataBind();
ViewState[“图书”]=图书;
}
受保护的无效非虚构点击(对象发送者,事件参数e)
{
Header_Label.Text=“非虚构部分”;
}
受保护的无效自\u帮助\u单击(对象发送者,事件参数e)
{
Header_Label.Text=“自助部分”;
}
受保护的void Sort_命令(对象发送器,CommandEventArgs e)
{
如果(例如,CommandName==“排序”)
{
开关(例如CommandArgument.ToString())
{
案例“ASC”:
图书分类(SortASC);
打破
案例“DESC”:
图书分类(SortDESC);
打破
}
}
if(ViewState[“books”]==null)
ViewState[“books”]=新字符串[0];
Item_Listbox.DataSource=新列表((字符串[])ViewState[“books”]);
Item_Listbox.DataBind();
}
}
此处引发的无效强制转换异常:

Item_Listbox.DataSource = new List<string>((string[])ViewState["books"]);
Item_Listbox.DataSource=新列表((字符串[])视图状态[“图书]);

我对ASP.NET还很陌生,所以我不知道是什么导致了它,欢迎

我想这是因为你

private List<String> books = new List<String>();
//...
ViewState["books"] = books;
用以下方法重写最后一行应该可以解决问题:

Item_Listbox.DataSource = (List<string>)ViewState["books"];
第二个地方有以下代码:

if (ViewState["books"] == null)
        ViewState["books"] = new string[0];
如果语句结果为false,因为在单击按钮后已经设置了ViewState,但通常我建议与您使用的数据结构保持一致,并将此代码更改为以下代码:

if (ViewState["books"] == null)
        ViewState["books"] = new List<string>();
if(ViewState[“books”]==null)
ViewState[“books”]=新列表();

您有两个地方可以设置“书籍”:
ViewState[“books”]=books其中
图书=新列表()


ViewState[“books”]=新字符串[0]

视图状态[“图书”]
列表
,而不是
字符串[]
。删除(字符串[]),它应该可以工作。事实上,您也可以删除新列表()。

该行应该做什么?难道你不想只看
Item\u Listbox.DataSource=ViewState[“books”]
?不需要强制转换。如果
ViewState[“books”]
被设置为
newstring[0]
,那么它就不起作用了。好吧,经过这一更正,我仍然遇到了我在另一个线程中发布的问题,当我点击按钮进行ASC或DESC时(在单击虚构按钮并填充列表框之后)然后,它将清除列表框,而不是将其按ASC或DESC顺序放置。谢谢你迄今为止的帮助!对这个错误有什么建议吗?
if (ViewState["books"] == null)
        ViewState["books"] = new string[0];
if (ViewState["books"] == null)
        ViewState["books"] = new List<string>();