ASP.NET 翻頁後繼續維持排序

來源:互聯網
上載者:User
asp.net|翻頁|排序

 要想實現翻頁後繼續排序,實現這樣的效果:

  比如共 15筆記錄,每頁顯示10條

  則排序時:第一頁將前10條記錄排序,翻第二頁時後五條再單獨排序.

  要注意以下幾點:

  1.如果資料很多,最好不要把資料集放到緩衝中(viewstate),影響效能

  2. ViewState中存放上次是哪個e.expression並且存放此e.expression是升序還是降序

  樣本如下:

  1.現有的排序事件是這樣寫的,這個是點擊上面排序標題時用:

private void grdProjTrace_SortCommand(object source, DataGridSortCommandEventArgs e)
{
 this.grdProjTrace.CurrentPageIndex = 0;
 DataView dv = 得到資料代碼;
 string strSort = "";
 string strOrder ="";//排序方式。0,降序,1升序
 if(ViewState["SortExpresstion"] != null)
 {
  strSort = ViewState["SortExpresstion"].ToString();
  strSort = strSort.Substring(0,strSort.Length -1);
  strOrder = ViewState["SortExpresstion"].ToString();
  strOrder = strOrder.Substring(strOrder.Length -1);
 }
 if(e.SortExpression == "CustomerName")
 {
  if(strSort != "CustomerName")
  {
   this.ViewState["SortExpresstion"] = ustomerName0";
   dv.Sort = "CustomerName DESC";
  }
  else
  {
   if(strOrder == "0")
   {
    this.ViewState["SortExpresstion"] = "CustomerName1";
    dv.Sort = "CustomerName ASC";
   }
   else
   {
    this.ViewState["SortExpresstion"] = "CustomerName0";
    dv.Sort = "CustomerName DESC";
   }
  }
 }
 if(e.SortExpression == "fullName")
 {
  if(strSort != "fullName")
  {
   this.ViewState["SortExpresstion"] = "fullName0";
   dv.Sort = "fullName DESC";
  }
  else
  {
   if(strOrder == "0")
   {
    this.ViewState["SortExpresstion"] = "fullName1";
    dv.Sort = "fullName ASC";
   }
   else
   {
    this.ViewState["SortExpresstion"] = "fullName0";
    dv.Sort = "fullName DESC";
   }
  }
 }
 this.grdProjTrace.DataSource = dv;
 this.grdProjTrace.DataBind();
}

  2.下面這個方法是自己寫的,翻頁事件中調用。

private void ChangePageDataBind()
{
 DataView dv = 得到資料代碼;
 string strSort = "";
 string strOrder ="";//排序方式。0,降序,1升序
 if(ViewState["SortExpresstion"] != null)
 {
  strSort = ViewState["SortExpresstion"].ToString();
  strSort = strSort.Substring(0,strSort.Length -1);
  strOrder = ViewState["SortExpresstion"].ToString();
  strOrder = strOrder.Substring(strOrder.Length -1);
 }
 if(this.ViewState["SortExpresstion"] != null)
 {
  if(strSort == "CustomerName")
  {
   if(strOrder == "1")
   {
    this.ViewState["SortExpresstion"] = "CustomerName1";
    dv.Sort = "CustomerName ASC";
   }
   else
   {
    this.ViewState["SortExpresstion"] = "CustomerName0";
    dv.Sort = "CustomerName DESC";
   }
  }
 }
 if(this.ViewState["SortExpresstion"] != null)
 {
  if(strSort == "fullName")
  {
   if(strOrder == "1")
   {
    this.ViewState["SortExpresstion"] = "fullName1";
    dv.Sort = "fullName ASC";
   }
   else
   {
    this.ViewState["SortExpresstion"] = "fullName0";
    dv.Sort = "fullName DESC";
   }
  }
 }
 this.grdProjTrace.DataSource = dv;
 this.grdProjTrace.DataBind();
}

  上面兩方法只要修改要排序的欄位名,就可以直接調用了.

  1、方法很簡單實用,這裡就不說了。

  2、方法是這樣用的:

private void grdProjTrace_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
 try
 {
  try
  {
   this.grdProjTrace.CurrentPageIndex = e.NewPageIndex;
  }
  catch
  {
   this.grdProjTrace.CurrentPageIndex = 0;
  }

  this.ChangePageDataBind();
 }
 catch(System.Exception errWS)
 {
  //異常
 }
}


 



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.