AspNetPager
foxd 2009-03-13 05:31:38 用AspNetPager分页。数据表中有1000多条记录,若不设查询条件的话AspNetPager显示有70多个分页。页面上有一个日期TextBox,两个DropDownList(部门、人员)和一个确定按钮。若日期、部门和人员都不选的话where变量是空字符串,就查出全部记录,但若选择了某个人员(如'张三')的话,点击确定按钮,显示的头一页确实是张三的记录,但后面李四、王五的记录仍然存在,还是70多页,不知AspNetPager应该怎样控制,即在改变查询条件(where中的内容有变化时),只显示应有的记录和页数。谢谢!
protected void gv_bind()
{
string sql = "Select originID, ......";
sql += where;
sql += " order by User_Name, time";
//xgkCon = db.get_xgk_con(); //指向巡更数据库连接
//if (xgkCon.State != ConnectionState.Open)
// xgkCon.Open();
apGridView.RecordCount = GetRecordCount(sql); //查到的记录数
SqlDataAdapter ada = new SqlDataAdapter(sql, SqlData.getcon());
DataSet ds = new DataSet();
ada.Fill(ds, apGridView.PageSize * (apGridView.CurrentPageIndex - 1), apGridView.PageSize, "table");
if (ds.Tables["table"].Rows.Count != 0)
{
this.GridView1.DataSource = ds.Tables["table"];
this.GridView1.DataBind();
}
else
{
this.GridView1.DataSource = null;
this.GridView1.DataBind();
}
}
protected int GetRecordCount(string sql)
{
DataTable dt = new DataTable();
dt = db.GetDataSet(sql, "table").Tables[0];
return dt.Rows.Count;
}
protected void apGridView_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
apGridView.CurrentPageIndex = e.NewPageIndex;
gv_bind();
}
protected void ddl_Department_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddl_Department.SelectedIndex == 0)
{
this.ddl_Person.Items.Clear();
}
else
{
string sql = "Select * from tb_User where User_Department_ID = " + this.ddl_Department.SelectedValue + "";
db.ddlDataBind(this.ddl_Person, sql, "User_Name", "User_ID");
this.ddl_Person.Items.Insert(0, "请选择");
}
}
protected void btn_OK_Click(object sender, EventArgs e) //确定
{
where = " where 1=1 ";
if (txt_Date1.Text != "" && ddl_Department.SelectedIndex == 0) //未选部门
where += " and [time] >= '" + this.txt_Date1.Text + "' and [time]< '" + Convert.ToDateTime(this.txt_Date1.Text).AddDays(1).ToShortDateString() + "'";
else if (txt_Date1.Text == "" && ddl_Department.SelectedIndex != 0) //选择了部门
{
if (this.ddl_Person.SelectedIndex == 0) //未选人员
where += " and Department_Name = '" + this.ddl_Department.SelectedItem.Text + "'";
else
where += " and Name = '" + this.ddl_Person.SelectedItem.Text + "'";
}
else if (txt_Date1.Text != "" && ddl_Department.SelectedIndex != 0)
where += " and [time] >= '" + this.txt_Date1.Text + "' and [time]< '" + Convert.ToDateTime(this.txt_Date1.Text).AddDays(1).ToShortDateString() + "' and Name = '" + this.ddl_Person.SelectedItem.Text + "'";
// this.apGridView.RecordCount = 0;
// this.apGridView.CurrentPageIndex = 0;
gv_bind();
}