2012-01-27 53 views
1

繼承人的整體代碼:無效的轉換異常ASP.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所以我迷路了至於什麼可能導致它,修復歡迎!

+1

那條線應該做什麼?你不想只是'Item_Listbox.DataSource = ViewState [「books」]'? – svick 2012-01-27 02:01:17

回答

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]; 

如果語句結果爲假,因爲ViewState的是按鈕,點擊後已經確定,但一般我會建議與您使用的數據結構保持一致,並將此代碼更改爲以下內容:

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

演員陣容沒有必要。對於ViewState [「books」]'設置爲'new string [0]''的情況,它將不起作用。 – svick 2012-01-27 02:05:19

+0

當然,編輯我的答案 – Restuta 2012-01-27 02:08:18

+0

好吧,有了這個更正,我仍然遇到了我在另一個線程中發佈的問題,當我點擊按鈕來執行ASC或DESC(點擊小說按鈕並填充列表框後),它會清除列表框而不是將其放入ASC或DESC順序。感謝你目前的幫助!有關此錯誤的任何建議,但? – user1062411 2012-01-27 03:33:50

0

您有2個地方你在哪裏設置「書籍」: ViewState["books"] = books;其中books = new List<String>();

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

0

ViewState["books"]List<string>,不string[]。刪除(字符串[]),它應該工作。實際上,你也可以刪除新的List()。