列表视图中的数据页不能工作
本文关键字:不能 工作 数据 视图 列表 | 更新日期: 2023-09-27 18:09:16
我在电子商务网站工作。我使用Listview控件填充产品。在一页上,我展示了36种产品。现在,如果我想用分页跳转到下一页,它就不起作用了。页面只停留在第一页。我试了两种方法
分页代码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页应用一些滤波器&如果行<那么它应该显示第1页的结果。在我下面的代码分页工作正常,但当上面的行场景我提到的发生,然后它显示空白页。>
protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
(products.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
this.populateProducts();
}
新 更新
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);
}
我猜你在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);
}
}
}