我正在尝试按其价格对数据进行排序(在数据列表中)。意味着有DropDownList供用户选择,如果他们想要从最昂贵到最便宜的价格,反之亦然。根据我的catID,存储在我的数据库中的价格是随机的。我已经编写了如下代码,但它没有根据我写的内容进行排序。我在这里做错了什么?请指教我。
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
bindDropDownList();
}
private void bindDropDownList()
{
DropDownList1.DataTextField = "price";
DataList1.DataSourceID = null;
DataList1.DataSource = getReader();
DropDownList1.DataBind();
}
private SqlDataReader getReader()
{
SqlDataReader reader = null;
if(DropDownList1.Text == "-Select-")
{
string strConnectionString =
ConfigurationManager.ConnectionStrings["ProBizConnection"].ConnectionString;
SqlConnection myConnect = new SqlConnection(strConnectionString);
string strCommandText ="SELECT * FROM [Category ] WHERE catID<= 20";
SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
myConnect.Open();
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
else if (DropDownList1.SelectedValue == "Price - Highest to Lowest")
{
string strConnectionString =
ConfigurationManager.ConnectionStrings["ProBizConnection"].ConnectionString;
SqlConnection myConnect = new SqlConnection(strConnectionString);
string strCommandText = "SELECT catID, packageName, price, description1, description2, image1, image2 FROM Category WHERE catID <= 20 ORDER BY price desc";
SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
myConnect.Open();
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
else if (DropDownList1.DataTextField == "Price - Lowest to Highest")
{
/string strConnectionString =
ConfigurationManager.ConnectionStrings["ProBizConnection"].ConnectionString;
SqlConnection myConnect = new SqlConnection(strConnectionString);
string strCommandText = "SELECT catID, packageName, price, description1, description2, image1, image2 FROM Category WHERE catID <= 20 ORDER BY price";
SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
myConnect.Open();
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
return reader;
}
我的.aspx代码:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" AppendDataBoundItems="true"
onselectedindexchanged="DropDownList1_SelectedIndexChanged" Height="18px"
Width="184px">
<asp:ListItem>-Select-</asp:ListItem>
<asp:ListItem>Price - Highest to Lowest</asp:ListItem>
<asp:ListItem>Price - Lowest to Highest</asp:ListItem>
</asp:DropDownList>
答案 0 :(得分:2)
排序不正常价格列可能是因为Price的数据类型不是小数而且可能是Varchar
因此,请将Price数据类型更改为Decimal以正确排序
答案 1 :(得分:0)
我可以看到三个错误......
1)您的getReader
代码有一条返回null的路径。您可能应该确保在其他检查为假的情况下,您的默认值始终为else
。
2)在尝试让读者进行数据绑定时,您正在读取DropDownList1.SelectedValue
的值。这看起来不对我 - 我假设有两个下拉列表,一个有订购信息,另一个是你想要设置和数据绑定到...
3)在getReader的elseif中,您要比较DropDownList1.DataTextField
,其中应包含您要绑定的字段的名称(您在{{1}的代码中将其设置为"price"
方法。
如果这些错误中的任何一个是您遇到的问题(如果它们现在不是将来它们可能将来),那么您应该能够通过逐步调试模式来发现这一点。这应该告诉你代码的路径是什么等等。
如果以上都不是导致您当前的问题,那么请提供更多详细信息。我不得不强调三个不同的问题,因为你的描述没有足够的说明你究竟出了什么问题。
http://tinyurl.com/so-hints是一个非常有用的链接,指向一个页面,试图帮助解释什么使问题变得好而且容易回答。
答案 2 :(得分:0)
我认为这一行是错误的,可能永远不会评估为真
else if (DropDownList1.DataTextField == "Price - Lowest to Highest")
应该是
else if (DropDownList1.SelectedValue == "Price - Lowest to Highest")
甚至更好
else if (DropDownList1.Text == "Price - Lowest to Highest")
<强>更新强>
AppendDataBoundItems="true"
可能会弄乱您的下拉项目。最低和最高的结果正在合并可能就是为什么你看不出差异。
删除默认列表项并将其放入代码隐藏
private void bindDropDownList()
{
DropDownList1.DataTextField = "price";
DropDownList1.DataSource = getReader();
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("-Select-"));
DropDownList1.Items.Insert(1, new ListItem("Price - Highest to Lowest"));
DropDownList1.Items.Insert(2, new ListItem("Price - Lowest to Highest"));
}
有关替代方法,请检查DropDownList AppendDataBoundItems (first item to be blank and no duplicates)