无法使用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();
    }