Listview中的Datapager无法正常工作

时间:2016-09-29 04:56:44

标签: c# asp.net listview

我在ecommmerce网站上工作。我使用Listview控件来填充产品。在一个页面上,我展示了36种产品。现在,如果我想使用分页转到下一页,那么它无法正常工作。页面仅停留在首页。我尝试了2种解决方案。

寻呼代码

protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
    if (IsPostBack) {
        DataPager pgr = products.FindControl("DataPager1") as DataPager;
        if (pgr != null && products.Items.Count != pgr.TotalRowCount) {
            pgr.SetPageProperties(0, pgr.MaximumRows, false);
        }
    }
}

我有很多CheckBoxList来过滤Listview数据。所以在这里我记住,如果用户在第10页,那么应用一些过滤器&如果行< 36然后它应该显示第1页的结果。在我的下面代码分页工作正常,但当我提到的上线情况发生时,它显示空白页。

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

更新PageLoad

try {
    query = "SELECT * FROM products where stock_status = @stock_status and status = @status";
    string conString = ConfigurationManager.ConnectionStrings("conio").ConnectionString;
    MySqlConnection con = new MySqlConnection(conString);
    MySqlCommand cmd = new MySqlCommand(query);
    cmd.Parameters.AddWithValue("@stock_status", "Ready Stock");
    cmd.Parameters.AddWithValue("@status", "active");
    con.Open();
    MySqlDataAdapter da = new MySqlDataAdapter();
    cmd.Connection = con;
    da.SelectCommand = cmd;
    DataTable dt = new DataTable();
    da.Fill(dt);
    ViewState("Data") = dt;
    products.DataSource = dt;
    products.DataBind();
    catHeading.Text = "Products In Ready Stock";
    itemCount.Text = dt.Rows.Count.ToString;
    catSliderHeader.Text = "Categories";
    Page.Title = "Ready Stock Products" + " | BrandSTIK";
    con.Close();
} catch (Exception ex) {
    Response.Write(ex);
}

1 个答案:

答案 0 :(得分:0)

我猜你没有在Page_Load事件中处理IsPostBack属性,并且列表视图在每次回发时都会刷新。请尝试以下代码。

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            try {
    query = "SELECT * FROM products where stock_status = @stock_status and status = @status";
    string conString = ConfigurationManager.ConnectionStrings("conio").ConnectionString;
    MySqlConnection con = new MySqlConnection(conString);
    MySqlCommand cmd = new MySqlCommand(query);
    cmd.Parameters.AddWithValue("@stock_status", "Ready Stock");
    cmd.Parameters.AddWithValue("@status", "active");
    con.Open();
    MySqlDataAdapter da = new MySqlDataAdapter();
    cmd.Connection = con;
    da.SelectCommand = cmd;
    DataTable dt = new DataTable();
    da.Fill(dt);
    ViewState("Data") = dt;
    products.DataSource = dt;
    products.DataBind();
    catHeading.Text = "Products In Ready Stock";
    itemCount.Text = dt.Rows.Count.ToString;
    catSliderHeader.Text = "Categories";
    Page.Title = "Ready Stock Products" + " | BrandSTIK";
    con.Close();
} catch (Exception ex) {
    Response.Write(ex);
}
        }
    }