ASP.NET填充列表框问题 [英] ASP.NET Populating Listbox issue

查看:83
本文介绍了ASP.NET填充列表框问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

截至目前,我根据其类别按钮尝试创建一个ASP.NET页面,它将从类别列出了书籍,一个列表框选择,然后我还有一个两个按钮(一个用于DESC顺序和一个用于ASC顺序)。现在的问题是,当我在ASC或DESC按钮点击击中小说按钮,并填充它抹列表框列表框后。

我张贴了类似的问题前,得到了一些解决方法,但它仍然是擦列表框,当我打的ASC或DESC按钮。

我是pretty新的ASP.NET那么简单或福利局友好的解释和code-例子/修补程序是非常欢迎!

在此先感谢!

code以下

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;公共部分类PARTIN:System.Web.UI.Page
{
私人列表<串GT;书=新的List<串GT;();无效Page_ preRender()
{
    Item_Listbox.DataSource =书籍;
    Item_Listbox.DataBind();
}INT SortASC(字符串x,y字符串)
{
    返回的String.Compare(X,Y);
}INT SortDESC(字符串x,y字符串)
{
    返回的String.Compare(X,Y)* -1;
}保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    如果(!的IsPostBack)
    {        Header_Label.Text =欢迎请选择一本书类别。
        Item_Listbox.DataSource =书籍;
        Item_Listbox.DataBind();    }}保护无效Fiction_Click(对象发件人,EventArgs的发送)
{        Header_Label.Text =小说部分;        (标题:老人与海|能解密:史诗小说|价格:10 USD |数量:3)books.Add;
        books.Add(标题:权力博弈|能解密:火与冰的故事|价格:15美元|数量:6);
        (标题:德古拉|能解密:一本关于吸血鬼|价格:5美元|数量:7)books.Add;
        books.Add(标题:暮光之城|能解密:一个可怕的书|价格:免费|数量:1000);        Item_Listbox.DataSource =书籍;
        Item_Listbox.DataBind();}
保护无效Non_Fiction_Click(对象发件人,EventArgs的发送)
{
    Header_Label.Text =非小说节;}
保护无效Self_Help_Click(对象发件人,EventArgs的发送)
{
    Header_Label.Text =自我帮助部分;}保护无效Sort_Command(对象发件人,CommandEventArgs E)
{
    如果(e.CommandName ==排序)
    {
        开关(e.CommandArgument.ToString())
        {
            案ASC:
                books.Sort(SortASC);
                打破;
            案DESC:
                books.Sort(SortDESC);
                打破;
        }    }
    Item_Listbox.DataSource =书籍;
    Item_Listbox.DataBind();
}}

ASPX code:

 &放大器; NBSP;< ASP:面板
        ID =导航=服务器HEIGHT =276pxWIDTH =197px
        的CssClass =导航BORDERCOLOR =黑色边框=双>
    < ASP:按钮的ID =小说=服务器文本=小说WIDTH =145px
            的CssClass =Nav_buttons的onclick =Fiction_Click/>
    < ASP:按钮的ID =Non_Fiction=服务器文本=非小说WIDTH =145px
            的CssClass =Nav_buttons的onclick =Non_Fiction_Click/>
    < ASP:按钮的ID =Self_Help=服务器文本=自我帮助WIDTH =145px
            的CssClass =Nav_buttons的onclick =Self_Help_Click/>
    < ASP:按钮的ID =NEW_ITEM=服务器文本=添加新项WIDTH =145px的CssClass =Nav_buttons/>
< / ASP:面板>
< ASP:面板ID =Books_Panel=服务器的CssClass =Books_PanelHEIGHT =409px
        边框样式=双>
        < ASP:标签ID =Header_Label=服务器
            风格=顶部:79px;左:693px;位置:绝对;高度:19px;宽度:234px
            文本=标签>< / ASP:标签>        < ASP:列表框ID =Item_Listbox=服务器
            风格=顶:204px;左:443px;位置:绝对;高度:136px;宽度:732px
            的AutoPostBack =真>
        < / ASP:列表框>        < ASP:按钮的ID =Ascending_Button=服务器
            风格=顶:375px;左:723px;位置:绝对;高度:26px;宽度:169px
            文本=升序的CommandName =排序CommandArgument =ASC
            按需=Sort_Command/>        < ASP:按钮的ID =Descending_Button=服务器
            风格=顶:405px;左:723px;位置:绝对;高度:26px;宽度:169px
            文本=降序的CommandName =排序CommandArgument =DESC
            按需=Sort_Command/>        < ASP:DropDownList的ID =Cat_Menu=服务器>
        < / ASP:DropDownList的>    < / ASP:面板>


解决方案

当你点击ASC或DESC按钮来排序它去进行排序处理,但这里的列表图书因此,空数据源绑定到列表页面上回发空的。

您应该​​重新绑定源或维持在一个视图状态,以便它可以在页面后回用。

尝试这样的事情。

 私人列表<串GT;图书
{
   得到{
         如果(的ViewState [书] == NULL){
             清单<串GT;书=新的List<串GT;();
             (标题:老人与海|能解密:史诗小说|价格:10 USD |数量:3)books.Add;
            books.Add(标题:权力博弈|能解密:火与冰的故事|价格:15美元|数量:6);
            (标题:德古拉|能解密:一本关于吸血鬼|价格:5美元|数量:7)books.Add;
            books.Add(标题:暮光之城|能解密:一个可怕的书|价格:免费|数量:1000);
             的ViewState [书] =书籍;
         }
         返回新的List<串GT;((字符串[])的ViewState [书]);
   }
   组{
       的ViewState [书] =值;
   }
}保护无效Fiction_Click(对象发件人,EventArgs的发送)
{
        Header_Label.Text =小说部分;        Item_Listbox.DataSource =书籍;
        Item_Listbox.DataBind();
}保护无效Sort_Command(对象发件人,CommandEventArgs E)
{
    如果(e.CommandName ==排序)
    {
        开关(e.CommandArgument.ToString())
        {
            案ASC:
                books.Sort(SortASC);
                打破;
            案DESC:
                books.Sort(SortDESC);
                打破;
        }    }    Item_Listbox.DataSource =书籍;
    Item_Listbox.DataBind();
}

As of right now I am trying to create an ASP.NET page which will list books from a category, in a listbox based upon which category button you choose and then I have another two buttons (one for DESC order and one for ASC order). Now the issue is when I click on the ASC or DESC button after hitting the fiction button and populating the list box it wipes the list box.

I posted a similar question earlier, got some fixes, but it is still wiping the listbox when I hit the ASC or DESC buttons.

I'm pretty new to ASP.NET so simple or "newb-friendly" explanations and code-examples/fixes are very welcome!

Thanks in advance!

Code Below

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 Code:

    &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>

解决方案

When you click on ASC or DESC button to sort the list it goes to sort handler but here books is empty on page postback so the empty datasource is bound to the list.

You should either rebind the source or maintain it in a viewstate so that it can be used on page post back.

Try something like this.

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

这篇关于ASP.NET填充列表框问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆