You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在GridView中通过DropDownList实现价格升序排序?现有代码无效求排查

Fixing Your GridView Price Sorting with DropDownList

Let's break down why your sorting isn't working and fix it step by step—there are a few key issues in your code that are preventing the GridView from updating:

1. You're not binding sorted data to the GridView

Right now, your code calls the stored procedure but never retrieves the results or connects them to your GridView. That's why nothing changes when you select "Low to High"—the sorted data is never sent to the GridView to display.

2. Your stored procedure has critical errors

  • You declared parameters (@price, @tourName, etc.) but don't use them at all. They're unnecessary here and just add confusion.
  • You're selecting the parameter variables instead of the actual columns from the Tours table. This would only return empty or uninitialized values, not the rows from your database.
  • Your ORDER BY prioritizes tourName over price—but you want to sort by price first. Swap those to get the low-to-high price order you need.

3. Database resources aren't being managed properly

You open the connection but don't guarantee it gets closed (if an error happens, the connection might stay open). Using using statements ensures connections, commands, and readers are automatically cleaned up.


Corrected Stored Procedure

CREATE PROCEDURE [dbo].[lowToHigh]
AS BEGIN
    -- Select actual table columns, sorted by price ascending (tourName as secondary sort)
    SELECT tourName, image, price, description 
    FROM Tours 
    ORDER BY price ASC, tourName ASC;
END

Corrected Backend Event Code

protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
    // First, make sure your DropDownList has AutoPostBack="True" in the markup!
    if (DropDownList.SelectedItem.Text == "Low to High")
    {
        // Replace "YourConnectionString" with your actual connection string (store this in web.config for best practice)
        using (SqlConnection con = new SqlConnection("YourConnectionString"))
        {
            con.Open();
            using (SqlCommand cmd = new SqlCommand("lowToHigh", con))
            {
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                
                // Get the sorted data from the stored procedure
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    // Bind the sorted data to your GridView (replace "YourGridViewID" with your actual GridView ID)
                    YourGridViewID.DataSource = reader;
                    YourGridViewID.DataBind();
                }
            }
        }
    }
}

Critical Markup Note

Don't forget to enable postback on your DropDownList—without this, the SelectedIndexChanged event won't fire when the user picks an option. Here's how your DropDownList markup should look:

<asp:DropDownList ID="DropDownList" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList_SelectedIndexChanged">
    <asp:ListItem Text="Low to High" Value="LowToHigh"></asp:ListItem>
    <!-- Add other sorting options here (e.g., "High to Low") -->
</asp:DropDownList>

内容的提问来源于stack exchange,提问作者depressedGirl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:05:51