AspNetPager分页控件启动url分页功能后,如何与搜索功能结合,不能使用Sessoin保持搜索条件sql语句。疑问:
想用ViewState对象保持搜索条件的sql语句,但是由于启动了url分页功能,所以在切换页码后,url被重置,页面的加载事件中if (!IsPostBack)总为true,导致ViewState对象总被初始化,页码切换后,显示的数据又变成了全部数据,而不是搜索所得到的数据。a.aspx页面后台代码
string sql = "select * from users";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) //url分页后总是为true,ViewState 总被重置为sql,搜索数据又变为全部数据
{
ViewState["sql"] = sql; //初始化ViewState["sql"]
Bind(ViewState["sql"].ToString());//绑定数据
}
}//搜索按钮
protected void btnSearch_Click(object sender, EventArgs e)
{
if (ViewState["sql"]==null)
{
ViewState["sql"] = sql;
} if (txtName.Text != "")
{
ViewState["sql"] += " where uname like '" + txtName.Text + "'";//保存查询语句
} Bind(ViewState["sql"].ToString());//绑定数据
}谁有方法解决这个问题????????
想用ViewState对象保持搜索条件的sql语句,但是由于启动了url分页功能,所以在切换页码后,url被重置,页面的加载事件中if (!IsPostBack)总为true,导致ViewState对象总被初始化,页码切换后,显示的数据又变成了全部数据,而不是搜索所得到的数据。a.aspx页面后台代码
string sql = "select * from users";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) //url分页后总是为true,ViewState 总被重置为sql,搜索数据又变为全部数据
{
ViewState["sql"] = sql; //初始化ViewState["sql"]
Bind(ViewState["sql"].ToString());//绑定数据
}
}//搜索按钮
protected void btnSearch_Click(object sender, EventArgs e)
{
if (ViewState["sql"]==null)
{
ViewState["sql"] = sql;
} if (txtName.Text != "")
{
ViewState["sql"] += " where uname like '" + txtName.Text + "'";//保存查询语句
} Bind(ViewState["sql"].ToString());//绑定数据
}谁有方法解决这个问题????????
你这能分页?bind方法里都没有传入一个AspNetPager1.CurrentPageIndex用viewstate可以不过一般都是存个条件。。比如:Load事件中声明一个ViewState["QureyWhere"] = string.Empty;写一个方法。。 public string GetWhere()
{
ViewState["QureyWhere"] = "这里是你的查询条件"; return ViewState["QureyWhere"].ToString();
}
private void DataBind()
{
string _cond = string.Empty;
_cond = " Where P_1001>0 ";
if (ddl_type.SelectedValue != "0")
{
string _childnum = pt.getChildNum(ddl_type.SelectedValue);
_childnum = ddl_type.Text + (_childnum == string.Empty ? _childnum : "," + _childnum);
_childnum = string.Format("'{0}'", _childnum.Replace(",", "','"));
_cond = _cond + string.Format(" and P_1102 in({0})", _childnum);
}
if (txt_keyword.Text.Trim() != string.Empty)
{
_cond = _cond + string.Format(" and (P_1002 like '%{0}%' or P_1003 like '%{0}%' or P_1013 like'%{0}%')", txt_keyword.Text);
}
if (Utils.IsDateString(DNTRequest.GetString("txt_startDate")))
{
_cond = _cond + string.Format(" and datediff(d,P_1014,'{0}') <= 0 ", DNTRequest.GetString("txt_startDate"));
}
if (Utils.IsDateString(DNTRequest.GetString("txt_endDate")))
{
_cond = _cond + string.Format(" and datediff(d,P_1014,'{0}') >= 0 ", DNTRequest.GetString("txt_endDate"));
}
int rowcount = 0;
rp_datalist.DataSource = pj.GetProjectList(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, " P_1014 DESC,P_1001 DESC ", _cond, out rowcount);
rp_datalist.DataBind();
AspNetPager1.RecordCount = rowcount;
}
protected void Button1_Click(object sender, EventArgs e)
{
DataBind();
} protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
DataBind();
}
然后数据一下子丢给它,就可以完成。 我拿3万条数据测试过。速度还是非常快。
他的效果就是url分页?page=1 page=2
#region 分页
/// <summary>
/// 分页方法
/// </summary>
/// <param name="pagesize">页数</param>
/// <param name="pageIndex">页码</param>
/// <param name="list">数据集合</param>
private void fenye(int pagesize, int pageIndex, List<LiuYan> list) {
PagedDataSource pd = new PagedDataSource();
pd.AllowPaging = true;
pd.PageSize = pagesize;
pd.DataSource = list;
this.anp.RecordCount = list.Count;
if (pageIndex >= pd.PageCount) {
pageIndex = pd.PageCount;
}
if (pageIndex <= 1) {
pageIndex = 1;
}
pd.CurrentPageIndex = pageIndex - 1; //if (list.Count == 0) { // string message = "<span color=#FF3300>没有相关信息,建议您更换搜索条件</span>";
//} this.RepeaterAdviceList.DataSource = pd;
this.RepeaterAdviceList.DataBind(); }
#endregion
session就免了,用了之后会有问题的,url参数怎么传,看了半天资料,改了半天,url毫无变化(用的是AspNetPager 7.2.0.0)
http://topic.csdn.net/u/20091021/11/c559459e-2370-4855-97d9-f2b6675831a6.html
page参数为aspnetpager自带的索引值。怎么修改aspnetpager的哪些相关属性值,才能传送后台的ViewState["sql"].ToString()?
不要钻牛角尖。作者在#8告诉你的是,简单的做法是使用“默认的Postback分页方式”。那么就不要揪住“url参数传”不放。先做完需要功能,等有闲功夫了再去搞什么“url参数传状态”之类的。你想,你知道当前页面有多少状态吗?1000个还是10000个?但是肯定很多。因为就算是一个TextBox控件,最少也可以有几十个状态数据由程序去动态修改(还不算用户使用Control.Attributes[...]自定义的状态数据)。那种说“url传状态给其它页面”的,是没有遇到过什么实际产品的开发,都是做个课堂练习了事。
默认的Postback分页方式。我是有需求才用url分页功能的,默认的Postback分页方式不能实现。需求是:在a页面中绑定一个编辑列,编辑列中传送参数(包括url分页的当前页索引,主键)给b页面,b页面编辑数据后,返回a页面原来的页码中去。
这必然要用到url分页才能够获取当前页的页码,才能返回原始的页码中去。js帖子:
http://topic.csdn.net/u/20110608/09/ff0a2b89-69e7-4a41-927a-12d291f0905e.html其实我也想用“默认的Postback分页方式”,然后b页面用js返回(history.back(),history.go(-n)),然后刷新a页面,但是发了个帖子,也是没有效果。
不一定非要用url分页的方式
aspnetpager启动了url分页,这个没有问题的,问题是结合搜索功能后,切换页码后,数据变成了全部数据,而不是搜索所得的数据了。