看了 aspnetpager分页控件的url分页的情况下根据查询结果动态分页的例子 也照着做了一下,大体上的思路是这样子的
点击查询按钮时
protected void ImageButton1_Click1(object sender, ImageClickEventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.Append("UserLists.aspx?Name=" + txtName.Text);
sb.Append("&Sex=" + ddlSex.SelectedValue); //根据输入的值和选择的条件拼接Url
Response.Redirect(sb.ToString());
}
在Page_Load中 :
protected void Page_Load(object sender, EventArgs e)
{
//if (!IsPostBack)
//{
// BindType();
//}
if(Request["Name"] !=null && Request["Name"] != "")
{
str.Append("UserName like '%");
str.Append(Request["Name"]+"%'");
}
if (Request["Sex"] !=null && Request["Sex"]!="")
{
str.Append(" and UserSex= ");
str.Append(Request["Sex"]);
}
AspNetPager1.RecordCount = GetRecordCount(str.ToString()); //根据查询条件查询出总的记录条数
}
由于是url分页 所以不用绑定数据,直接在AspNetPager1_PageChanging 中绑定数据
protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
AspNetPager1.CurrentPageIndex = e.NewPageIndex;
int cpage = 0;
if (Request.QueryString["page"] != null)
{
cpage = Convert.ToInt32(Request.QueryString["page"].ToString());
}
else
{
cpage = 1;
}
int rowscount = 0;
txtName.Text = Request["Name"];
ddlSex.SelectedValue = Request["Sex"]; //显示查询条件
GridView1.DataSource = news(str.ToString(), "UserID", cpage, 10, out rowscount);
GridView1.DataBind();
}