在分页页面上应用过滤器显示空结果

时间:2016-09-01 05:44:26

标签: c# asp.net listview

我对分页页面应用过滤器有疑问。我使用ListView Control来显示来自数据库和数据库的数据。有一些过滤器。现在问题是假设如果我查看所有产品(每页18个产品)&拥有187个产品所以如果我切换到第7页&然后应用价格范围过滤器,然后它找到该范围内的产品,但显示空结果。这是我的实时网站链接http://foxboxretail.in/products

我的Datapager代码

protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
    (products.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
    products.PageIndex = 0;
    buildWhereClause();
}

过滤应用代码

public string buildWhereClause()
{
    string pageName = string.Empty;    
    string constr = ConfigurationManager.ConnectionStrings("conio2").ConnectionString;
    string query = "select * from products";
    string joiner = "";

    string condition = string.Empty;
    string whereClause = string.Empty;
    string priceCondition = string.Empty;

    try {
        for (i = 0; i <= priceFilter.Items.Count - 1; i++) {
            if (priceFilter.Items(i).Selected) {
                string price = priceFilter.Items(i).ToString;
                priceCondition = string.Concat(priceCondition, joiner, string.Format("'{0}'", price));
                if (string.IsNullOrEmpty(joiner))
                    joiner = ",";
            }
        }

        joiner = " where ";
        if (!string.IsNullOrEmpty(priceCondition)) {
            whereClause = string.Concat(whereClause, joiner, string.Format("price_range IN ({0})", priceCondition));
            joiner = " and ";
        }


        string masterClause = string.Empty;
        if (whereClause == string.Empty) {
            masterClause = (query + " Where type = @type and status = @status");
        } else {
            masterClause = (query + whereClause + " and type = @type and status = @status");
        }

        using (MySqlConnection con = new MySqlConnection(constr)) {
            using (MySqlCommand cmd = new MySqlCommand(masterClause)) {
                using (MySqlDataAdapter sda = new MySqlDataAdapter(cmd)) {
                    cmd.Parameters.AddWithValue("@type", "product");
                    cmd.Parameters.AddWithValue("@status", "active");
                    cmd.Connection = con;
                    using (DataTable dt = new DataTable()) {
                        sda.Fill(dt);
                        products.DataSource = dt;
                        products.DataBind();
                        itemCount.Text = dt.Rows.Count.ToString;
                    }
                }
            }
        }
    } catch (Exception ex) {
        Response.Write(ex);
        return string.Concat(query, whereClause);
    }
}

更新(datapager的快照) enter image description here

2 个答案:

答案 0 :(得分:0)

结果计数表示找到了记录(我应用了“Rs.501 - Rs.1000”过滤器并且有33个结果 - 至少这是标签显示的内容)但是该列表正试图显示第7页(并且只有2个)

尝试在过滤数据后重置页面索引。

答案 1 :(得分:0)

Change the PageIndex property of the gridview to the first page or the last page whenever you are changing the filters. Remember to bind the gridview after changing the pageindex

To set the page index to first page:

mygridView.PageIndex = 0;

As you are using the DataPager for paging, you need to set as follows after the buildWhereClause :

(products.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, true);

Set the last input as true in the SetPagerProperties