环境:windows server standard 2003 ,asp.net2.0,IIS
问题:如何用asp.net的GridView控件分页显示带查询条件的数据
具体描述:
aspx页是个带条件的查询,结果分页显示,刚载入页面时全部数据分页显示各页跳转都没问题,选择了条件再查,第1页的数据没问题,但点第2页时显示的却是不按条件查时第2页显示的数据,再点第1页时第1页也变成不带条件查询时第一页的数据了,查询条件不起作用了。
解决办法:用ViewState把上次带查询条件的SQL语句保存起来,然后在分页事件中把该语句取出来,执行。
//自定义方法
private void bindGridView(String p_cmdtext)
{
ConnectionManager connectionManager = new ConnectionManager(Request.ApplicationPath);
SqlConnection conn = connectionManager.getConnection();
SqlDataAdapter sda = new SqlDataAdapter(p_cmdtext, conn);
DataSet ds = new DataSet();
sda.Fill(ds, "search");
GridView1.DataSource = ds.Tables["search"];
GridView1.DataKeyNames = new string[] { "student_no" };
this.GridView1.DataBind();
this.ViewState.Remove("SEARCH_SQL");
this.ViewState.Add("SEARCH_SQL", p_cmdtext);
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex= e.NewPageIndex;
String cmdtext = "";
cmdtext = (String)this.ViewState["SEARCH_SQL"];
this.bindGridView(cmdtext);//自定义方法
}
参考:
http://topic.csdn.net/u/20080311/13/9cb61a9d-e4b1-48ee-8052-ddc04945fffb.html?61712749