AspNetPager搜索后分页的难题
如题,我用post传值方式搜索后里用AspNetPager来分页(两种模式:一种是AspNetPager不显示?page参数,另一种是Url分页即带参数?page=),我贴出我的代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindListData();
BindHot();
}
}
/// <summary>
/// 数据绑定--初始化
/// </summary>
private void BindListData()
{
DataSet ds1 = NewsBll.GetList("*", " Title Like '%" + Request.Form["txtKeyWord"].Trim()+ "%'", " ID DESC");//这里返回个DataSet ,问题:显示我第二页显示的时候Request.Form["txtKeyWord"]为空了,取的的数据条件就是like '%%'所有数据的第二页了,这是不显示?page参数的分页方式。带参数page的呢,又如何处理?请指教
AspNetPager1.RecordCount = ds1.Tables[0].Rows.Count;
Session["dvlist"] = ds1.Tables[0].DefaultView;
bindData();
}
/// <summary>
/// 数据绑定
/// </summary>
private void bindData()
{
if (ZsRequest.GetInt("page", 0) == 0)//ZsRequest类无须关注,我用来接收参数的一个封装类相当于 Request.QueryString("page")
{
CurrentPage = 1;
}
else
{
CurrentPage = Convert.ToInt32(ZsRequest.GetString("page"));
}
PagedDataSource pds = new PagedDataSource();
pds.AllowPaging = true;
pds.PageSize = AspNetPager1.PageSize;
pds.CurrentPageIndex = CurrentPage - 1;
pds.DataSource = (DataView)Session["dvlist"];
RPNewsList.DataSource = pds;
RPNewsList.DataBind();
}
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
bindData();
}
我下面还有第二种写法,虽然不带page的分页功能实现了,但还是有个问题:当点击第二页时关键字的搜索我要在页面上显示,却显示不了,很显然是点击第二页时Request.Form["txtKeyWord"]为空了
const string vsKey = "searchCriteria"; //ViewState key
protected string strKeyWord = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
strKeyWord = Request.Form["txtKeyWord"].Trim();
string strQuery = " Title Like '%" + strKeyWord + "%'";
ViewState[vsKey] = strQuery;
searchOrders(strQuery);
}
}
void searchOrders(string sWhere)
{
DataSet ds1 = NewsBll.GetList("*", sWhere, " ID DESC");
AspNetPager1.RecordCount = ds1.Tables[0].Rows.Count;
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds1.Tables[0].DefaultView;
pds.AllowPaging = true;
pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
pds.PageSize = AspNetPager1.PageSize;
RPNewsList.DataSource = pds;
RPNewsList.DataBind();
}
protected void AspNetPager1_PageChanged(object src, EventArgs e)
{
searchOrders((string)ViewState[vsKey]);
}
最后的问题,求搜索后的url分页即带参数?page=的方式的解决办法