ASP.NET填充列表框问题

截至目前我正在尝试创建一个ASP.NET页面,它将根据您选择的类别按钮在列表框中列出类别的书籍,然后我有另外两个按钮(一个用于DESC订单,一个用于ASC订单) 。 现在的问题是,当我点击小说按钮并填充列表框后点击ASC或DESC按钮时,它会擦除​​列表框。

我之前发布了一个类似的问题,得到了一些修复,但是当我点击ASC或DESC按钮时它仍在擦除列表框。

我是ASP.NET的新手,非常欢迎这么简单或“newb友好”的解释和代码示例/修复!

提前致谢!

代码如下

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 books = new List(); 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代码:

                  

当您单击ASC或DESC按钮对列表进行排序时,它将转到排序处理程序,但此处books在页面回发时为空,因此空数据源将绑定到列表。

您应该重新绑定源或将其保存在视图状态中,以便可以在页面回发时使用它。

尝试这样的事情。

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