无法使用DropDownList按数据库价格对从数据库中获取的值进行排序 [英] Unable to sort out the values taken from database by its price using DropDownList

查看:55
本文介绍了无法使用DropDownList按数据库价格对从数据库中获取的值进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过使用DropDownList通过从数据库中获取所有值,按照价格从最贵到最便宜的顺序对我的包裹进行排序.默认情况下,用户已经可以看到我插入到数据库中的所有软件包.但是,我仍然得到与用户看到的默认输出相同的输出,因此当我选择下拉列表时,该输出不会根据价格进行分类.我在这里做错了什么?

这是我的.aspx文件:

I am trying to sort my packages according to its price from the most expensive to the cheapest using DropDownList by getting all values from the database. And by default, users can already see all the packages that i have inserted into the database. However, i still get the same output as the default seen by the user whereby it is not sorted out accordingly to the price when i select the dropdownlist. What have i done wrong here?

This is my .aspx file:

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" AppendDataBoundItems="true" 
                onselectedindexchanged="DropDownList1_SelectedIndexChanged">
                <asp:ListItem>Choose one</asp:ListItem>
                <asp:ListItem>Price - high to low</asp:ListItem>
                <asp:ListItem>Price - low to high</asp:ListItem>
            </asp:DropDownList>



这是我的.cs文件:



This is my .cs file:

protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            bindDropDownList();
        }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
    
    }

    private void bindDropDownList()
    {

        DropDownList1.DataSource = getReader();
        DropDownList1.DataBind();

    }

    private SqlDataReader getReader()
    {
        
        string strConnectionString =
            ConfigurationManager.ConnectionStrings["ToysConnectionString"].ConnectionString;
        SqlConnection myConnect = new SqlConnection(strConnectionString);

        string strCommandText = "SELECT package, price, description, image1, image2 FROM catalogue WHERE catalogueID <= 10 ORDER BY price desc";

        SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
        myConnect.Open();
        
        SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        return reader;
    }

推荐答案


更改代码
change you code from
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }









to

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList();
    }


这篇关于无法使用DropDownList按数据库价格对从数据库中获取的值进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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