C# ASP.NET填充列表框问题
目前,我正在尝试创建一个ASP.NET页面,该页面将根据您选择的类别按钮在列表框中列出某个类别的书籍,然后我有另外两个按钮(一个用于描述订单,一个用于ASC订单)。现在的问题是,当我点击虚构按钮并填充列表框后,点击ASC或DESC按钮时,它会擦除列表框 我之前发布了一个类似的问题,得到了一些修复,但当我点击ASC或DESC按钮时,它仍在清除列表框 我对ASP.NET还很陌生,所以非常欢迎简单或“新手友好”的解释和代码示例/修复 提前谢谢 代码如下C# ASP.NET填充列表框问题,c#,asp.net,listbox,C#,Asp.net,Listbox,目前,我正在尝试创建一个ASP.NET页面,该页面将根据您选择的类别按钮在列表框中列出某个类别的书籍,然后我有另外两个按钮(一个用于描述订单,一个用于ASC订单)。现在的问题是,当我点击虚构按钮并填充列表框后,点击ASC或DESC按钮时,它会擦除列表框 我之前发布了一个类似的问题,得到了一些修复,但当我点击ASC或DESC按钮时,它仍在清除列表框 我对ASP.NET还很陌生,所以非常欢迎简单或“新手友好”的解释和代码示例/修复 提前谢谢 代码如下 using System; using Syst
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();
}
}
使用系统;
使用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();
}
受保护的无效非虚构点击(对象发送者,事件参数e)
{
Header_Label.Text=“非虚构部分”;
}
受保护的无效自\u帮助\u单击(对象发送者,事件参数e)
{
Header_Label.Text=“自助部分”;
}
受保护的void Sort_命令(对象发送器,CommandEventArgs e)
{
如果(例如,CommandName==“排序”)
{
开关(例如CommandArgument.ToString())
{
案例“ASC”:
图书分类(SortASC);
打破
案例“DESC”:
图书分类(SortDESC);
打破
}
}
Item_Listbox.DataSource=书籍;
Item_Listbox.DataBind();
}
}
ASPX代码:
<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>
当您单击ASC或DESC按钮对列表进行排序时,它会转到排序处理程序,但此处的
books
在回发页面上为空,因此空数据源绑定到列表
您应该重新绑定源文件或将其保持在viewstate中,以便在回发页面时使用
试试这样的
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();
}
私人图书目录
{
得到{
if(ViewState[“books”]==null){
列表书籍=新列表();
书籍。添加(“标题:老人与海|描述:史诗小说|价格:10美元|数量:3”);
书籍。添加(“标题:权力游戏|描述:火与冰的故事。|价格:15美元|数量:6”);
书籍。添加(“标题:德古拉|描述:一本关于吸血鬼的书。|价格:5美元|数量:7”);
书籍。添加(“标题:暮光之城|描述:一本糟糕的书。|价格:免费|数量:1000”);
ViewState[“图书”]=图书;
}
返回新列表((字符串[])ViewState[“books”]);
}
设置{
ViewState[“books”]=值;
}
}
受保护的无效链接单击(对象发送者,事件参数e)
{
Header_Label.Text=“虚构部分”;
Item_Listbox.DataSource=书籍;
Item_Listbox.DataBind();
}
受保护的void Sort_命令(对象发送器,CommandEventArgs e)
{
如果(例如,CommandName==“排序”)
{
开关(例如CommandArgument.ToString())
{
案例“ASC”:
图书分类(SortASC);
打破
案例“DESC”:
图书分类(SortDESC);
打破
}
}
Item_Listbox.DataSource=书籍;
Item_Listbox.DataBind();
}
当您单击ASC或DESC按钮对列表进行排序时,它会转到排序处理程序,但此处的books
在回发页面上为空,因此空数据源绑定到列表
您应该重新绑定源文件或将其保持在viewstate中,以便在回发页面时使用
试试这样的
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();
}
私人图书目录
{
得到{
if(ViewState[“books”]==null){
列表书籍=新列表();
书籍。添加(“标题:老人与海|描述:史诗小说|价格:10美元|数量:3”);
书籍。添加(“标题:权力游戏|描述:火与冰的故事。|价格:15美元|数量:6”);
书籍。添加(“标题:德古拉|描述:一本关于吸血鬼的书。|价格:5美元|数量:7”);
书籍。添加(“标题:暮光之城|描述:一本糟糕的书。|价格:免费|数量:1000”);
ViewState[“图书”]=图书;
}
返回新列表((字符串[])ViewState[“books”]);
}
设置{
ViewState[“books”]=值;
}
}
受保护的无效链接单击(对象发送者,事件参数e)
{
Header_Label.Text=“虚构部分”;
Item_Listbox.DataSource=书籍;
Item_Listbox.DataBind();
}
受保护的void Sort_命令(对象发送器,CommandEventArgs e)
{
如果(例如,CommandName==“排序”)
{
开关(例如CommandArgument.ToString())
{
案例“ASC”:
图书分类(SortASC);
打破
案例“DESC”:
图书分类(SortDESC);