熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

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

2022-06-13   來源: .NET編程 

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

  比如共 筆記錄每頁顯示

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

  要注意以下幾點:

  如果數據很多最好不要把數據集放到緩存中(viewstate)影響性能

   ViewState中存放上次是哪個eexpression並且存放此eexpression是升序還是降序

  示例如下:

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

private void grdProjTrace_SortCommand(object source DataGridSortCommandEventArgs e)
{
 thisgrdProjTraceCurrentPageIndex = ;
 DataView dv = 得到數據代碼;
 string strSort = ;
 string strOrder =;//排序方式降序升序
 if(ViewState[SortExpresstion] != null)
 {
  strSort = ViewState[SortExpresstion]ToString();
  strSort = strSortSubstring(strSortLength );
  strOrder = ViewState[SortExpresstion]ToString();
  strOrder = strOrderSubstring(strOrderLength );
 }
 if(eSortExpression == CustomerName)
 {
  if(strSort != CustomerName)
  {
   thisViewState[SortExpresstion] = ustomerName;
   dvSort = CustomerName DESC;
  }
  else
  {
   if(strOrder == )
   {
    thisViewState[SortExpresstion] = CustomerName;
    dvSort = CustomerName ASC;
   }
   else
   {
    thisViewState[SortExpresstion] = CustomerName;
    dvSort = CustomerName DESC;
   }
  }
 }
 if(eSortExpression == fullName)
 {
  if(strSort != fullName)
  {
   thisViewState[SortExpresstion] = fullName;
   dvSort = fullName DESC;
  }
  else
  {
   if(strOrder == )
   {
    thisViewState[SortExpresstion] = fullName;
    dvSort = fullName ASC;
   }
   else
   {
    thisViewState[SortExpresstion] = fullName;
    dvSort = fullName DESC;
   }
  }
 }
 thisgrdProjTraceDataSource = dv;
 thisgrdProjTraceDataBind();
}
>
  下面這個方法是自己寫的翻頁事件中調用

private void ChangePageDataBind()
{
 DataView dv = 得到數據代碼;
 string strSort = ;
 string strOrder =;//排序方式降序升序
 if(ViewState[SortExpresstion] != null)
 {
  strSort = ViewState[SortExpresstion]ToString();
  strSort = strSortSubstring(strSortLength );
  strOrder = ViewState[SortExpresstion]ToString();
  strOrder = strOrderSubstring(strOrderLength );
 }
 if(thisViewState[SortExpresstion] != null)
 {
  if(strSort == CustomerName)
  {
   if(strOrder == )
   {
    thisViewState[SortExpresstion] = CustomerName;
    dvSort = CustomerName ASC;
   }
   else
   {
    thisViewState[SortExpresstion] = CustomerName;
    dvSort = CustomerName DESC;
   }
  }
 }
 if(thisViewState[SortExpresstion] != null)
 {
  if(strSort == fullName)
  {
   if(strOrder == )
   {
    thisViewState[SortExpresstion] = fullName;
    dvSort = fullName ASC;
   }
   else
   {
    thisViewState[SortExpresstion] = fullName;
    dvSort = fullName DESC;
   }
  }
 }
 thisgrdProjTraceDataSource = dv;
 thisgrdProjTraceDataBind();
}

  上面兩方法只要修改要排序的字段名就可以直接調用了

  方法很簡單實用這裡就不說了

  方法是這樣用的:

private void grdProjTrace_PageIndexChanged(object source DataGridPageChangedEventArgs e)
{
 try
 {
  try
  {
   thisgrdProjTraceCurrentPageIndex = eNewPageIndex;
  }
  catch
  {
   thisgrdProjTraceCurrentPageIndex = ;
  }

  thisChangePageDataBind();
 }
 catch(SystemException errWS)
 {
  //異常
 }
}

From:http://tw.wingwit.com/Article/program/net/201311/11882.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.