截至目前,我正在尝试创建一个ASP.NET页面,该页面将列出来自类别的书籍,列表框中的基于您选择哪个类别的按钮,然后我有另外两个按钮(一个用于DESC订单,另一个用于ASC订单)。现在的问题是,当我点击小说按钮并点击了ASC或DESC按钮并填充列表框时,它将清除列表框并将我引回页面加载事件。如果/ else语句ASP.NET C#
我已经尝试将填充项移动到页面加载事件,并且当我拥有所有内容时都工作得很完美,但由于某种原因,通过其他按钮点击时它不起作用。
我对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>();
public 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)
{
Header_Label.Text = "Welcome! Please select a book category.";
}
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");
}
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;
}
}
}
}
编辑:谢谢你的职位,不再径直回到了页面加载事件,并保持在标签的变化说“小说”,但它仍然是正在重置的数据当我点击ASD或DESC按钮时列表框。
谢谢寻求帮助!特别是回发问题,使页面不再重新加载到页面加载事件中列出的内容,但是当我使用ASC或DESC按钮时,它仍然正在清除列表框。 – user1062411
当您在排序方法中放置断点时,“书籍”列表可能为空。您可以将该列表存储在SessionState中以避免此问题。 – Koen