ASP.NET處理資料分頁(2)

來源:互聯網
上載者:User
asp.net|分頁|資料 四. 第二種分頁瀏覽資料記錄的關鍵步驟以及實現方法: 其實這二種分頁方法在程式設計中是大同小異的,在第二種方法中,其前面的關鍵步驟中和第一種分頁幾乎相同,也是要得到瀏覽資料記錄的總數,設定每一頁要顯示的資料記錄個數,計算出總共有多少資料頁面等等。這些步驟的實現方法可以參考第一種方法。第二種分頁方法和第一種分頁方法的主要區別在於資料導航的實現方法上。下列代碼功能是實現第二種分頁方法資料導航:  

Response.Write ( ' <p > 資料導航: ' )
  nPageEnd = nPage + 3
  If nPageEnd > nPageCount
  nPageEnd = nPageCount
  End If
  For i = 1 To nPageEnd
  If i = nPage Then
  Response.Write ( ' <b > ' & i.ToString ( ) & ' </b > ' )
  Else
  Response.Write ( '<A HREF = ''' & SCRIPT_NAME & _
  '?Page=' & ( i ).ToString ( )  & _
  ''' > ' & i.ToString ( ) & '</A > ' )
  End If
  Next
  
  If nPageEnd < nPageCount Then
  Response.Write ( '<A HREF = ''' & SCRIPT_NAME & _
  '?Page=' & ( nPageEnd + 1 ).ToString ( ) & _
  ''' >更多...</A > ' )
  End If  


五. 第二種分頁瀏覽資料記錄的完整來源程式代碼(no2.aspx):  

no2.aspx和no1.aspx在程式設計的思想和方法上大致相同,下面是no2.aspx的來源程式,具體如下:  

<% @ Page Language = 'VB' %>
  <% @ Import Namespace = 'System.Data' %>
  <% @ Import Namespace = 'System.Data.OleDb' %>
  
  <script runat = 'server' >
  Const Record_Per_Page   As Short = 5 '定義每一頁顯示的記錄數
  Private Script_Name As String  

Sub Page_Load ( Source As Object , e As EventArgs )
  Script_Name = GetPageName ( )
  '第二種方式來分頁顯示資料
  ShowRecords ( )
  End Sub
  
  '得到起始瀏覽超連結字串
  Function GetPageName ( ) As String
  Dim Str As String
  Dim Pos As Short
  Str = Request.ServerVariables ( 'Script_Name' ).Trim ( )
  Pos = Str.LastIndexOf ( '/' )
  If Pos >= 0 Then
  Return Str.SubString ( Pos + 1 )
  Else
  Return Str
  End If
  End Function
  
  Private Sub ShowRecords ( )
  Dim strConn As String '定義資料連線字串  
Dim SQL As String  '定義SQL語句
  Dim odConn As OleDbConnection
  Dim odAdapt As OleDbDataAdapter
  Dim DS As DataSet '建立DataSet對象
  Dim DT As DataTable '建立DataTable對象
  Dim nRecCount As Integer '儲存記錄總數
  Dim nPageCount As Integer '儲存總共的資料頁面數目
  Dim nPage As Integer '存放要瀏覽當前資料頁面號
  Dim nStart As Integer '存放當前頁面的起始記錄序號
  Dim nEnd As Integer '存放當前頁面的終止記錄序號
  Dim nPageEnd As Integer '儲存當前頁面的最後一面的序號
  Dim i As Integer  
'確認要瀏覽的頁面序號
nPage = Convert.ToInt32 ( Request.QueryString ( 'Page' ) )
  SQL = 'SELECT * FROM tblItem '
  
  '建立資料連線字串
  strConn = ' Provider = Microsoft.Jet.OLEDB.4.0 ; ' & _
  ' Data Source = ' & Server.MapPath ( 'data.mdb' ) & ' ; ' & _
  ' User ID = ; Password = ; '
  Try
  '得到資料記錄總數
  odConn = New OleDbConnection ( strConn )
  odAdapt = New OleDbDataAdapter ( SQL , odConn )
  DS = New DataSet
  odAdapt.Fill ( DS )
  DT = DS.Tables ( 0 )
  nRecCount = DT.Rows.Count
  Catch e As Exception
  Response.Write('錯誤資訊: <b >' & e.Message & '</b > <p > ' )
  nRecCount = 0
  End Try
  
  If nRecCount > 0 Then
  ' 確定資料記錄要顯示的頁面數
  nPageCount = nRecCount \ Record_Per_Page
  If nRecCount Mod Record_Per_Page > 0 Then
  nPageCount += 1
  End If
  
  '確認瀏覽命令中的頁面參數是否越界,如果越界則重設頁面序號
  If nPage < 1  Then
  nPage = 1  
End If
  If  nPage > nPageCount Then
  nPage = nPageCount  
End If
  
  Response.Write ( '總共有資料記錄' & nRecCount.ToString ( ) & ' 條' & '。<br >' )
  Response.Write(' <p > <b >第二種分頁顯示為:</b > <p > ' )
  
  '確認當前頁面的開始記錄和終止記錄
  nStart = Record_Per_Page *  ( nPage - 1 )
  nEnd = nStart + Record_Per_Page - 1
  If nEnd > nRecCount - 1 Then
  nEnd = nRecCount - 1
  End If
  '在螢幕中輸出記錄
  For i = nStart To nEnd
  Response.Write ( DT.Rows ( i ) ( 'ItemName' ) & ' <br > ' )
  Next
  End If
  Response.Write ( ' <p > 資料導航: ' )
  nPageEnd = nPage + 3
  If nPageEnd > nPageCount
  nPageEnd = nPageCount
  End If
  For i = 1 To nPageEnd
  If i = nPage Then
  Response.Write ( ' <b > ' & i.ToString ( ) & ' </b > ' )
  Else
  Response.Write ( '<A HREF = ''' & SCRIPT_NAME & _
  '?Page=' & ( i ).ToString ( )  & _
  ''' > ' & i.ToString ( ) & '</A > ' )
  End If
  Next
  
  If nPageEnd < nPageCount Then
  Response.Write ( '<A HREF = ''' & SCRIPT_NAME & _
  '?Page=' & ( nPageEnd + 1 ).ToString ( ) & _
  ''' >更多...</A > ' )
  End If
  End Sub
  </script >  


本文介紹的這二種分頁瀏覽記錄類型雖然採用的資料庫都是本機資料庫,但對其他類型的資料庫也是一樣適用的,這隻需要修改一下資料連線字串就可以實現了,譬如如果採用了SQL Server資料庫。此SQL Server資料庫伺服器是'Server1',資料庫是'Data',使用者名稱為預設的'sa',沒有設定密碼。只需要把上面二段程式中的字串'strConn'變換成:  

strConn = 'Provider = SQLOLEDB.1 ; Persist Security Info = False ; User ID = sa ; Initial Catalog = Data ; Data Source = server1 '  


就可以實現了。  

六. 總結:  

本文介紹的二種分頁瀏覽資料記錄方法在ASP.NET資料庫編程方面是非常有用的,因為在資料處理方面,分頁顯示記錄比起其他的一些處理,譬如:資料修改、刪除等都要難些。希望上面的這些內容對你利用ASP.NET開發資料庫程式有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.