如果类别链接是导航,如何按与其关联的类别搜索产品。
但是,当我点击某些类别时,它没有显示与该类别相关的产品,从字面上显示所有产品。
例如,如果我单击Starters,则只有starters产品不会显示所有产品。
我正在使用datalist来显示产品。我一直试图从2晚以来对此进行梳理并进行了大量研究,但找不到我需要的具体内容。
你能帮我解决这个问题吗?
我在我的产品页面中使用此编码,但它无法正常工作
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
if (Request.QueryString["category"] ==null)
{
cmd.CommandText = "select * from Products";
}
else
{
cmd.CommandText = "select * from Products where product_category='" + Request.QueryString["category"].ToString() + "'";
}
cmd.CommandText = "select * from Products";
cmd.ExecuteNonQuery();
DataTable dtt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dtt);
DataList1.DataBind();
con.Close();
你有3个问题:
在你的Exploits of a Mom阻止之后删除cmd.CommandText = "select * from Products";
。并设置数据源:
else
我自己整理了这个。只是为了帮助这是我的代码,它适用于我。
if (Request.QueryString["category"] ==null)
{
cmd.CommandText = "select * from Products";
}
else
{
//use parameters to prevent SQL injection
cmd.CommandText = "select * from Products where product_category=@category";
cmd.Parameters.Add(new SqlParameter("category", Request.QueryString["category"]));
}
//cmd.CommandText = "select * from Products";<--- delete this line
/*.. exeute quesry and load into datatable */
//bind datalist:
DataList1.DataSource = dtt;
DataList1.DataBind();
在if语句之后,您仍然设置命令文本以选择所有产品,因此请注释cmd.CommandText =“select * from Products”;在if语句之后再试一次