如何在c#中使用querystring过滤具有多个参数的列表视图


ProductList.aspx
<asp:DataList ID="dl_category" runat="server">
<ItemTemplate>
<li>
<a href = "ProductList.aspx?ProdCategory=<%#Eval("Category_Name")%>"><%# Eval("Category_Name")%></a>
</li>
</ItemTemplate>
</asp:DataList>
<asp:DataList ID="dl_vendor" runat="server">
<ItemTemplate>
<li>
<a href = "ProductList.aspx?Vendor=<%# Eval("Vendor_Name")%>"><%# Eval("Vendor_Name")%></a>
</li>
</ItemTemplate>
</asp:DataList>

目前我有2个数据。一个数据列表按产品类别过滤listview,而另一个数据列表按供应商名称过滤listview。

ProductList.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
loadcategory();
loadvendorname();
filter();
}
}
protected void filter()
{
if (Request.QueryString["ProdCategory"] != null)
{
string productcategory = Request.QueryString["ProdCategory"];
string vendorname = null;
List<Product> filterprodList = new List<Product>();
conn.Open();
string queryStr = "select * from products where product_category='" + productcategory + "'";
SqlCommand cmd = new SqlCommand(queryStr, conn);
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
lv_Product.DataSourceID = null;
lv_Product.DataSource = dt;
lv_Product.DataBind();
conn.Close();
}
if (Request.QueryString["Vendor"] != null)
{
string productcategory = null;
string vendorname = Request.QueryString["Vendor"];
List<Product> filterprodList = new List<Product>();
filterprodList = aProd.filterProductAll(productcategory, vendorname);
lv_Product.DataSourceID = null;
lv_Product.DataSource = filterprodList;
lv_Product.DataBind();
}
if (Request.QueryString["ProdCategory"] != null && Request.QueryString["Vendor"] != null)
{
string productcategory = Request.QueryString["ProdCategory"];
string vendorname = Request.QueryString["Vendor"];
List<Product> filterprodList = new List<Product>();
filterprodList = aProd.filterProductAll(productcategory, vendorname);
lv_Product.DataSourceID = null;
lv_Product.DataSource = filterprodList;
lv_Product.DataBind();
}

然而,目前我只能通过一个参数过滤listview,因为我当前的href一次只能接受一个参数(例如ProductList.aspx?Vendor=<%# Eval("Vendor_Name"))我怎样才能使它一次可以过滤2个或更多的参数?谢谢您的宝贵时间。D

我希望能够得到一个url的例子(ProductList.aspx ? ProdCategory = Category&供应商= Vendor1)

一个经常被忽视的问题是,sqlCommand对象不需要在添加参数之前构建sql,或者实际上不需要更改所使用的sql !

所以,这里的目标应该是没有,1、2、3甚至5个参数传递。如果没有传递任何参数,那么不要使用或包含该"缺乏"的任何条件。

把问题反过来。我们只为传递的参数添加条件。

这样,您可以扩展此代码-它将适用于2或20个值。事实上,由于这里的每个代码存根都是"重复的",那么我想我们可以将重复的代码分解成一个单独的例程。在这个例子中,我们将0-3。

这种方法也意味着我们处理所有可能值的排列(组合)。如前所述,我们通过简单地忽略和不包含非传递值来实现这一点。

所以,你的过滤器代码应该看起来像这样:
public void MyFilterTest()
{
string strSQL = "SELECT * FROM products";
string strWhere = "";
SqlCommand cmdSQL = new SqlCommand("", new SqlConnection(My.Settings.Test3));
if (Request.QueryString("ProdCategory") != Null)
{
strWhere = "(Product_Catagorory = @Pcata)";
cmdSQL.Parameters.Add("@pcata", SqlDbType.NVarChar).Value = Request.QueryString("ProdCategory");
}
if (Request.QueryString("Vendor") != Null)
{
if (strWhere != "") strWhere += " AND ";
strWhere += "(VENDORNAME = @VendName)";
cmdSQL.Parameters.Add("@VendName", SqlDbType.NVarChar).Value = Request.QueryString("Vendor");
}
if (Request.QueryString("CompanyName") != Null)
{
if (strWhere != "") strWhere += " AND ";
strWhere += "(CompanyName = @CompName)";
cmdSQL.Parameters.Add("@CompName", SqlDbType.NVarChar).Value = Request.QueryString("CompanyName");
}
if (strWhere != "") strSQL += " WHERE " + strWhere;
cmdSQL.CommandText = strSQL;
cmdSQL.Connection.Open();
DataTable MyTable = new DataTable();
MyTable.Load(cmdSQL.ExecuteReader);
lv_Product.DataSource = MyTable;
lv_Product.DataBind();
}

有两个技巧:您不必事先固定cmdSQL对象和参数。参数的创建和添加;可以100%独立于命令对象的SQL发生——在您实际为命令对象创建SQL之前,它们不知道或不关心彼此——这可以在添加参数之后发生!

下一个技巧:我们构建sqlWhere子句。如果前面的where没有值,则不追加"one_answers";子句到strWhere。因此,如果我们没有,或20 -这也适用!如:

if (strWhere != "") strWhere += " AND ";

是的-我把代码放在一行-这是我的派对,如果我想的话我会哭或者把这样的一行放在一行代码里!!

所以,在你"前进"的过程中建立标准。未传递的值根本不会成为最终的"where"子句,因此这将消除"组合"。所有可能的值。我们根本不在乎。如上所述,这3个示例代码存根实际上很可能被写成一个小函数,因为这3个代码存根看起来非常相似——但不管怎样——你明白了。还要注意sqlCommand对象有3个元素:

A connection object (don't need a separate one).
A sql text - we could have in fact used cmdSQL object in place of strSQL
A execute reader - so we only need that one object + a data table - not separate objects

最新更新