0

I have a problem with paginated pages to apply filter. I have used ListView Control to display data from database & having some filters. Now problem is suppose If I view all products (18 Products per page) & having 187 Products So If I switch to 7th page & then apply price range filter then it find products from that range but showing empty result. This is my live site Link http://foxboxretail.in/products

My Datapager Code

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

Filter Applying Code

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);
    }
}

UPDATE (Snapshot of datapager) enter image description here

SUN
  • 973
  • 14
  • 38

2 Answers2

0

The results count says the records are found (I applied a "Rs.501 - Rs.1000" filter and there were 33 results - at least that is what the label shown) but the list is trying to show the 7th page (and there are only 2)

Try to reset the page index after filtering data.

owczarek
  • 335
  • 2
  • 10
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

Richa Garg
  • 1,888
  • 12
  • 23