2012-01-27 108 views
1

截至目前,我正嘗試創建一個ASP.NET頁面,該頁面將列出來自類別的書籍,列表框中的列表框基於您選擇的類別按鈕,然後我還有另外兩個按鈕(一個對於DESC訂單和一個用於ASC訂單)。現在的問題是當我點擊小說按鈕並填充列表框擦除列表框時單擊ASC或DESC按鈕。ASP.NET填充列表框問題

我之前發佈了一個類似的問題,得到了一些修復,但是當我點擊ASC或DESC按鈕時它仍然在清除列表框。

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

} 


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

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



} 

ASPX代碼:

&nbsp;<asp:Panel 
     ID="Navigation" runat="server" Height="276px" Width="197px" 
     CssClass="Navigation" BorderColor="Black" BorderStyle="Double"> 
    <asp:Button ID="Fiction" runat="server" Text="Fiction" Width="145px" 
      CssClass="Nav_buttons" onclick="Fiction_Click" /> 
    <asp:Button ID="Non_Fiction" runat="server" Text="Non-Fiction" Width="145px" 
      CssClass="Nav_buttons" onclick="Non_Fiction_Click" /> 
    <asp:Button ID="Self_Help" runat="server" Text="Self Help" Width="145px" 
      CssClass="Nav_buttons" onclick="Self_Help_Click" /> 
    <asp:Button ID="New_Item" runat="server" Text="Add New Item" Width="145px" CssClass="Nav_buttons" />   
</asp:Panel> 
<asp:Panel ID="Books_Panel" runat="server" CssClass="Books_Panel" Height="409px" 
     BorderStyle="Double"> 
     <asp:Label ID="Header_Label" runat="server" 
      style="top: 79px; left: 693px; position: absolute; height: 19px; width: 234px" 
      Text="Label"></asp:Label> 

     <asp:ListBox ID="Item_Listbox" runat="server" 


      style="top: 204px; left: 443px; position: absolute; height: 136px; width: 732px" 
      AutoPostBack="True"> 
     </asp:ListBox> 

     <asp:Button ID="Ascending_Button" runat="server" 
      style="top: 375px; left: 723px; position: absolute; height: 26px; width: 169px" 
      Text="Ascending Order" CommandName="Sort" CommandArgument="ASC" 
      OnCommand="Sort_Command" /> 

     <asp:Button ID="Descending_Button" runat="server" 
      style="top: 405px; left: 723px; position: absolute; height: 26px; width: 169px" 
      Text="Descending Order" CommandName="Sort" CommandArgument="DESC" 
      OnCommand="Sort_Command" /> 

     <asp:DropDownList ID="Cat_Menu" runat="server"> 
     </asp:DropDownList> 

    </asp:Panel> 
+0

你能發佈你的aspx標記嗎? – ShankarSangoli 2012-01-27 00:38:01

+0

被添加到原始帖子 – user1062411 2012-01-27 04:13:11

回答

1

當您在ASC或DESC按鈕來排序它去進行排序處理程序列表中,但這裏books是網頁提交空的,所以在空數據源綁定到列表。

您應該重新綁定源代碼或將其保留在視圖狀態中,以便在頁面回發時使用它。

嘗試類似這樣的東西。

private List<String> books 
{ 
    get{ 
     if(ViewState["books"] == null){ 
      List<String> books = new List<String>(); 
      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"); 
      ViewState["books"] = books; 
     } 
     return new List<String>((String[])ViewState["books"]); 
    } 
    set{ 
     ViewState["books"] = value; 
    } 
} 

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

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

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

    } 

    Item_Listbox.DataSource = books; 
    Item_Listbox.DataBind(); 
} 
+0

正如我所說我是一個新手,從我所瞭解的視角狀態是有道理的,因爲當我走過它,似乎數據沒有去哪裏。我嘗試了你的建議,但我現在收到這個錯誤: – user1062411 2012-01-27 01:49:27

+0

Invaild拋出異常 – user1062411 2012-01-27 01:50:30

+0

嘗試我編輯的答案,在鑄造時將'string'改爲'String'。 – ShankarSangoli 2012-01-27 03:35:12