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();
}
...全文
387 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
iloveaspx 2009-09-09
  • 打赏
  • 举报
回复
不错

在线演示和源码下载地址

http://aspnetpager.51aspx.com/
puzhichen 2009-08-26
  • 打赏
  • 举报
回复
神奇地发现 楼主的回帖率超过了100%
datahandler2 2009-03-14
  • 打赏
  • 举报
回复
不知AspNetPager应该怎样控制,即在改变查询条件(where中的内容有变化时),只显示应有的记录和页数。
=================
这个就要看你的数据源怎么获取了。跟aspnetpager控件没多大关系。可参考下人家的SQL存储过程写法----里面内含动态组成查询条件式。
或你也可以代码中动态组成条件
teerhu 2009-03-14
  • 打赏
  • 举报
回复
分页时不要重复查询。

protected void gv_bind()
{
string sql = "Select originID, ......";
sql += where;
sql += " order by User_Name, time";


apGridView.RecordCount = GetRecordCount(sql); //查到的记录数

SqlDataAdapter ada = new SqlDataAdapter(sql, SqlData.getcon());
DataSet ds = new DataSet();

ada.Fill(ds,"table");
DataTable dt=ds.Tables["table"];
ViewState["dt"]=dt;

if (dt.Rows.Count != 0)
{
this.GridView1.DataSource = dt.DefaultView;
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)
{
GridView1.CurrentPageIndex = e.NewPageIndex;
gvPageBind();
}
protected void gvPageBind()
{
DataTable dt =(DataTable)ViewState["dt"];
this.GridView1.DataSource = dt.DefaultView;
this.GridView1.DataBind();
}

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();
}
foxd 2009-03-13
  • 打赏
  • 举报
回复
不知怎地where中的内容变化后仍然还是70多页,比如where = 'name = “张三"'头一页显示确实是张三,后面别的人也还在。

62,267

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧