GridView Sorting 分页 使用GridView的Sorting事件,在分页的时候,点击第二页,之前单击表头呈现的排序效果又没有了。怎么样才能在分页的时候依然能保持之前的排序效果。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 分页方法需要自己写,不然分页就不能保持排序状态。protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) { if (e.SortExpression != "") { if (GridView1.PageCount > 0) { string SortDirection = "ASC"; SortDirection = (this.GridView1.Attributes["SortDirection"].ToString() == SortDirection ? "DESC" : "ASC"); this.GridView1.Attributes["SortExpression"] = e.SortExpression; this.GridView1.Attributes["SortDirection"] = SortDirection; //重新绑定数据 DataBind(); } } } 如果楼主是使用自带的分页,建议楼主使用SqlDataSource,或者如果分层的话,使用ObjectDataSource,只要设置一下GridView1.AllowSorting = true;即OK了! //数据绑定 public void DataBound(string _WhereStr) { //防止刷新 if (this.isRefresh()) { return; } if (string.IsNullOrEmpty(_WhereStr)) { _WhereStr = " 1=1"; } string aidSelect = "SELECT * from emp,dep where emp.dep_id = dep.dep_id and emp_type = '1' and " + _WhereStr; DataTable fileTable = new DataTable(); fileTable = DbAccess.GetDS(aidSelect).Tables[0]; if (fileTable.Rows.Count == 0) { //Response.Write("<script>alert('没有符合此条件的记录!')</script>"); //DbUITool.JsMsg(this.Page, "没有符合此条件的记录!"); return; } else { GridView1.DataSource = fileTable; GridView1.DataBind(); } this.lblCurrentPage.Text = string.Format("当前第{0}页/总共{1}页", this.GridView1.PageIndex + 1, this.GridView1.PageCount); } //GridView分页 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; tiaojian = this.lbtiaojian.Text.Trim(); DataBound(tiaojian); } 你首先在pageload里面定义 一个ViewState["sort"]="" DataSet ds_todayjzb = tTodayJzbBLL.queryTodayJzbByCondition(condition); if (ViewState["sort"] == "") { ds_todayjzb.Tables[0].DefaultView.Sort = " jzbh desc"; } else { ds_todayjzb.Tables[0].DefaultView.Sort = ViewState["sort"].ToString(); } gv_todayjzb.DataSource = ds_todayjzb.Tables[0].DefaultView; gv_todayjzb.DataBind(); 一次删除7张不关联表的数据 iframe显示问题 如何在ASP.NET中播放视频,我想做一个土豆网那种类型的网站,希望各位好心人帮帮忙! 请为后台C#代码,如何访问HTML客户端控件? ASP.NET下什么控件可以让搜索结果像BAIDU等搜索引擎那样? 水晶报表中的切换字段视图按钮~~~ 高分求教!错误 '800a03f6' 关于字符处理的问题 班竹,还有各位大侠,急问两个很久都悬而未决的问题,请帮帮忙! 疑惑:dataset的用法 gridview与DetailsView结合显示数据不相符?? 疑难杂病:asp.net Sql Server 2005 无法插入数据
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
if (e.SortExpression != "")
{
if (GridView1.PageCount > 0)
{
string SortDirection = "ASC";
SortDirection = (this.GridView1.Attributes["SortDirection"].ToString() == SortDirection ? "DESC" : "ASC");
this.GridView1.Attributes["SortExpression"] = e.SortExpression;
this.GridView1.Attributes["SortDirection"] = SortDirection;
//重新绑定数据
DataBind();
}
}
}
SqlDataSource,或者如果分层的话,使用ObjectDataSource,
只要设置一下GridView1.AllowSorting = true;即OK了!
public void DataBound(string _WhereStr)
{
//防止刷新
if (this.isRefresh())
{
return;
}
if (string.IsNullOrEmpty(_WhereStr))
{
_WhereStr = " 1=1";
}
string aidSelect = "SELECT * from emp,dep where emp.dep_id = dep.dep_id and emp_type = '1' and " + _WhereStr;
DataTable fileTable = new DataTable();
fileTable = DbAccess.GetDS(aidSelect).Tables[0]; if (fileTable.Rows.Count == 0)
{
//Response.Write("<script>alert('没有符合此条件的记录!')</script>");
//DbUITool.JsMsg(this.Page, "没有符合此条件的记录!");
return;
}
else
{
GridView1.DataSource = fileTable;
GridView1.DataBind();
}
this.lblCurrentPage.Text = string.Format("当前第{0}页/总共{1}页", this.GridView1.PageIndex + 1, this.GridView1.PageCount);
}
//GridView分页
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
tiaojian = this.lbtiaojian.Text.Trim();
DataBound(tiaojian);
}
if (ViewState["sort"] == "")
{
ds_todayjzb.Tables[0].DefaultView.Sort = " jzbh desc";
}
else
{
ds_todayjzb.Tables[0].DefaultView.Sort = ViewState["sort"].ToString();
}
gv_todayjzb.DataSource = ds_todayjzb.Tables[0].DefaultView;
gv_todayjzb.DataBind();