ASP.NET處理瀏覽器中數(shù)據(jù)分頁的方法
發(fā)表時(shí)間:2024-06-11 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]在ASP的數(shù)據(jù)庫編程的時(shí),由于瀏覽器的大小限制,在要瀏覽的數(shù)據(jù)記錄比較多的時(shí)候,為了達(dá)到更直觀的效果,我們把這些數(shù)據(jù)記錄分成若干的頁面,通過數(shù)據(jù)導(dǎo)航按鈕(或者其他超鏈接),分頁的瀏覽。其實(shí)這種數(shù)據(jù)記錄的分頁瀏覽在ASP.NET也能夠?qū)崿F(xiàn)。并且在實(shí)現(xiàn)的過程中比起在ASP的處理過程顯得條理更清晰,也更...
在ASP的數(shù)據(jù)庫編程的時(shí),由于瀏覽器的大小限制,在要瀏覽的數(shù)據(jù)記錄比較多的時(shí)候,為了達(dá)到更直觀的效果,我們把這些數(shù)據(jù)記錄分成若干的頁面,通過數(shù)據(jù)導(dǎo)航按鈕(或者其他超鏈接),分頁的瀏覽。其實(shí)這種數(shù)據(jù)記錄的分頁瀏覽在ASP.NET也能夠?qū)崿F(xiàn)。并且在實(shí)現(xiàn)的過程中比起在ASP的處理過程顯得條理更清晰,也更容易些。 通過瀏覽器進(jìn)行分頁瀏覽數(shù)據(jù)記錄基本類型主要有二種。其他類型的分頁瀏覽要么是對(duì)這二種類型的修改,要么是對(duì)這二種類型的綜合。具體表現(xiàn)方式的如下面這二幅圖:
圖01:第一種分頁瀏覽數(shù)據(jù)記錄式樣
圖02:第二種分頁瀏覽數(shù)據(jù)記錄式樣 下面就來探討一下在ASP.NET中這二種分頁瀏覽數(shù)據(jù)記錄的具體實(shí)現(xiàn)過程: 首先來介紹一下我們使用的數(shù)據(jù)庫,在本文中為了方便起見,我們使用了本地?cái)?shù)據(jù)庫Access 2000,數(shù)據(jù)庫名稱為"Data.mdb",里面存放了一張數(shù)據(jù)表"tblItem"。此數(shù)據(jù)表的結(jié)構(gòu)如下: 字段名稱字段類型ItemID自動(dòng)編號(hào)ItemName文本類型如果你采用的是別的數(shù)據(jù)庫,只需對(duì)下面介紹的程序進(jìn)行簡(jiǎn)單的修改就可以了。這將在下面介紹。 一. 本文程序設(shè)計(jì)和運(yùn)行的軟件環(huán)境: (1).微軟公司視窗2000服務(wù)器版 (2)..Net FrameWork SDK Beta 2 二. 第一種分頁瀏覽數(shù)據(jù)記錄的關(guān)鍵步驟以及實(shí)現(xiàn)方法: (1).首先要得到初始瀏覽數(shù)據(jù)記錄的超鏈接字符串: 這其實(shí)很關(guān)鍵,因?yàn)樵诘谝环N分頁瀏覽中的"首頁"、"下一頁"等操作,都是通過在這個(gè)超鏈接字符串后面加入要瀏覽頁面的參數(shù)來實(shí)現(xiàn)的,在本文的程序中是通過GetPageName ( )函數(shù)來實(shí)現(xiàn)的。此函數(shù)具體如下: 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 (2).要得到你所要瀏覽的數(shù)據(jù)記錄總數(shù): 在本文中,為了方便,我們是把數(shù)據(jù)表"tblItem"中的全部記錄都拿來瀏覽。ASP.NET頁面通過ADO.NET來得到數(shù)據(jù)表"tblItem"。下面代碼就是利用ADO.NET來得到"tblItm"表中記錄總數(shù)的程序代碼: <% @ Page Language = "VB" %>
<% @ Import Namespace = "System.Data" %>
<% @ Import Namespace = "System.Data.OleDb" %>
<script runat = "server" >
Dim strConn As String '定義數(shù)據(jù)連接字符串
Dim SQL As String '定義SQL語句
Dim odConn As OleDbConnection
Dim odAdapt As OleDbDataAdapter
Dim DS As DataSet '創(chuàng)建DataSet對(duì)象
Dim DT As DataTable '創(chuàng)建DataTable對(duì)象
Dim nStart As Integer '存放當(dāng)前頁面的起始記錄序號(hào)
Dim nEnd As Integer '存放當(dāng)前頁面的終止記錄序號(hào)
Dim i As Integer
'確認(rèn)要瀏覽的頁面序號(hào)
nPage = Convert.ToInt32 ( Request.QueryString ( "Page" ) )
SQL = "SELECT * FROM tblItem "
'創(chuàng)建數(shù)據(jù)連接字符串
strConn = " Provider = Microsoft.Jet.OLEDB.4.0 ; " & _
" Data Source = " & Server.MapPath ( "data.mdb" ) & " ; " & _
" User ID = ; Password = ; "
Try
'得到數(shù)據(jù)記錄總數(shù)
odConn = New OleDbConnection ( strConn )
odAdapt = New OleDbDataAdapter ( SQL , odConn )
DS = New DataSet
odAdapt.Fill ( DS )
DT = DS.Tables ( 0 )
'得到數(shù)據(jù)記錄總數(shù)
nRecCount = DT.Rows.Count
Catch e As Exception
Response.Write("錯(cuò)誤信息: <b>" & e.Message & "</b><p>")
nRecCount = 0
End Try
</script > (3).計(jì)算出瀏覽的數(shù)據(jù)記錄總共頁面數(shù): 在瀏覽頁面中,我們發(fā)現(xiàn)了每一頁只瀏覽5條記錄,你可以通過修改程序中定義一個(gè)常量"Record_Per_Page"來改變每一頁瀏覽數(shù)據(jù)記錄的個(gè)數(shù)。在知道了要瀏覽數(shù)據(jù)記錄的總數(shù)后,通過下面代碼來計(jì)算出要顯示這些數(shù)據(jù)記錄所需要的頁面總數(shù): Const Record_Per_Page As Short = 5 '定義每一頁顯示的記錄數(shù)
Dim nPageCount As Integer '保存總共的數(shù)據(jù)頁面數(shù)目
Dim nPage As Integer '存放要瀏覽當(dāng)前數(shù)據(jù)頁面號(hào)
nPageCount = nRecCount Record_Per_Page
If nRecCount Mod Record_Per_Page > 0 Then
nPageCount += 1
End If
'確認(rèn)瀏覽命令中的頁面參數(shù)是否越界,如果越界則重置頁面序號(hào)
If nPage < 1 Then
nPage = 1
End If
If nPage > nPageCount Then
nPage = nPageCount
End If (4).數(shù)據(jù)導(dǎo)航的實(shí)現(xiàn)方法: 其實(shí)數(shù)據(jù)導(dǎo)航是通過對(duì)參數(shù)"Page"賦值來實(shí)現(xiàn)的,其中程序中的"nPage"是當(dāng)前數(shù)據(jù)頁面序號(hào), "nPageCount"是數(shù)據(jù)頁面的總和。下面是實(shí)現(xiàn)這些數(shù)據(jù)導(dǎo)航的具體實(shí)現(xiàn)代碼: Response.Write ( " <p >數(shù)據(jù)導(dǎo)航:<A HREF = """ & Script_Name & _
"?Page=" & ( 1 ).ToString ( ) & _
""">首 頁 </A >" )
Response.Write( " " )
'瀏覽"上一頁"處理辦法
Response.Write ( " <A HREF = """ & Script_Name & _
"?Page=" & ( nPage - 1 ).ToString ( ) & _
""" >上一頁</A > " )
Response.Write ( " " )
'瀏覽"下一頁"處理辦法
Response.Write ( "<A HREF =""" & Script_Name & _
"?Page=" & ( nPage + 1 ).ToString ( ) & _
""" >下一頁</A > " )
Response.Write ( " " )
'瀏覽"尾頁"處理辦法
Response.Write ( "<A HREF = """ & Script_Name & _
"?Page=" & ( nPageCount ).ToString ( ) & _
""" >尾 頁</A > " )
'顯示當(dāng)前頁和合計(jì)頁數(shù)
Response.Write ( " " & "頁次:"& nPage.ToString ( ) & "/" & nPageCount.ToString ( ) & " <br > " ) (5).顯示不同頁面的數(shù)據(jù)記錄: 根據(jù)超鏈接字符串得到"Page"值,然后根據(jù)此值來得到在此頁面中要顯示的起始記錄號(hào)和結(jié)束記錄號(hào),再通過一個(gè)循環(huán)把這些記錄給顯示出來。下面這行代碼是讀取參數(shù)"Page": nPage = Convert.ToInt32 ( Request.QueryString ( "Page" ) ) 下面這些代碼是根據(jù)用戶想要去的頁面得到起始記錄號(hào)和結(jié)尾記錄號(hào),并通過屏幕顯示出來: Dim nStart As Integer '存放當(dāng)前頁面的起始記錄序號(hào)
Dim nEnd As Integer '存放當(dāng)前頁面的終止記錄序號(hào)
Dim i As Integer
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
在本文介紹的二種分頁瀏覽記錄類型中,對(duì)于數(shù)據(jù)顯示,我們采用了簡(jiǎn)化處理。我們知道在瀏覽器上,瀏覽記錄一般都是通過DbGrid的形式來實(shí)現(xiàn)的,這一點(diǎn)在其實(shí)也好實(shí)現(xiàn),讀者只需稍微修改一下本文中的程序代碼就可以實(shí)現(xiàn)。 三. 第一種分頁瀏覽數(shù)據(jù)記錄的完整程序代碼(no1.aspx): 組織一下上面的這些步驟的實(shí)現(xiàn)方法,可以得到下列完整代碼: <% @ Page Language = "VB" %>
<% @ Import Namespace = "System.Data" %>
<% @ Import Namespace = "System.Data.OleDb" %>
<script runat = "server" >
Const Record_Per_Page As Short = 5 '定義每一頁顯示的記錄數(shù)
Private Script_Name As String
Sub Page_Load ( Source As Object , e As EventArgs )
Script_Name = GetPageName ( )
'第一種方式來分頁顯示數(shù)據(jù)
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
'此函數(shù)的功能是分頁顯示數(shù)據(jù)庫中的記錄
Private Sub ShowRecords ( )
Dim strConn As String '定義數(shù)據(jù)連接字符串
Dim SQL As String '定義SQL語句
Dim odConn As OleDbConnection
Dim odAdapt As OleDbDataAdapter
Dim DS As DataSet '創(chuàng)建DataSet對(duì)象
Dim DT As DataTable '創(chuàng)建DataTable對(duì)象
Dim nRecCount As Integer '保存記錄總數(shù)
Dim nPageCount As Integer '保存總共的數(shù)據(jù)頁面數(shù)目
Dim nPage As Integer '存放要瀏覽當(dāng)前數(shù)據(jù)頁面號(hào)
Dim nStart As Integer '存放當(dāng)前頁面的起始記錄序號(hào)
Dim nEnd As Integer '存放當(dāng)前頁面的終止記錄序號(hào)
Dim i As Integer
'確認(rèn)要瀏覽的頁面序號(hào)
nPage = Convert.ToInt32 ( Request.QueryString ( "Page" ) )
SQL = "SELECT * FROM tblItem "
'創(chuàng)建數(shù)據(jù)連接字符串
strConn = " Provider = Microsoft.Jet.OLEDB.4.0 ; " & _
" Data Source = " & Server.MapPath ( "data.mdb" ) & " ; " & _
" User ID = ; Password = ; "
Try
'得到數(shù)據(jù)記錄總數(shù)
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("錯(cuò)誤信息: <b>" & e.Message & "</b><p>")
nRecCount = 0
End Try
'判斷是否存在數(shù)據(jù)記錄
If nRecCount > 0 Then
' 確定數(shù)據(jù)記錄要顯示的頁面數(shù)
nPageCount = nRecCount Record_Per_Page
If nRecCount Mod Record_Per_Page > 0 Then
nPageCount += 1
End If
'確認(rèn)瀏覽命令中的頁面參數(shù)是否越界,如果越界則重置頁面序號(hào)
If nPage < 1 Then
nPage = 1
End If
If nPage > nPageCount Then
nPage = nPageCount
End If
Response.Write ( "總共有數(shù)據(jù)記錄" & nRecCount.ToString ( ) & " 條" & "。<br >" )
Response.Write(" <p > <b >第一種分頁顯示為:</b > <p > " )
'確認(rèn)當(dāng)前頁面的開始記錄和終止記錄
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 >數(shù)據(jù)導(dǎo)航:<A HREF = """ & Script_Name & _
"?Page=" & ( 1 ).ToString ( ) & _
""">首 頁 </A >" )
Response.Write( " " )
'瀏覽"上一頁"處理辦法
Response.Write ( " <A HREF = """ & Script_Name & _
"?Page=" & ( nPage - 1 ).ToString ( ) & _
""" >上一頁</A > " )
Response.Write ( " " )
'瀏覽"下一頁"處理辦法
Response.Write ( "<A HREF =""" & Script_Name & _
"?Page=" & ( nPage + 1 ).ToString ( ) & _
""" >下一頁</A > " )
Response.Write ( " " )
'瀏覽"尾頁"處理辦法
Response.Write ( "<A HREF = """ & Script_Name & _
"?Page=" & ( nPageCount ).ToString ( ) & _
""" >尾 頁</A > " )
'顯示當(dāng)前頁和合計(jì)頁數(shù)
Response.Write ( " " & "頁次:"& nPage.ToString ( ) & "/" & nPageCount.ToString ( ) & " <br > " )
End Sub
</script > 四. 第二種分頁瀏覽數(shù)據(jù)記錄的關(guān)鍵步驟以及實(shí)現(xiàn)方法: 其實(shí)這二種分頁方法在程序設(shè)計(jì)中是大同小異的,在第二種方法中,其前面的關(guān)鍵步驟中和第一種分頁幾乎相同,也是要得到瀏覽數(shù)據(jù)記錄的總數(shù),設(shè)定每一頁要顯示的數(shù)據(jù)記錄個(gè)數(shù),計(jì)算出總共有多少數(shù)據(jù)頁面等等。這些步驟的實(shí)現(xiàn)方法可以參考第一種方法。第二種分頁方法和第一種分頁方法的主要區(qū)別在于數(shù)據(jù)導(dǎo)航的實(shí)現(xiàn)方法上。下列代碼功能是實(shí)現(xiàn)第二種分頁方法數(shù)據(jù)導(dǎo)航: Response.Write ( " <p > 數(shù)據(jù)導(dǎo)航: " )
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 五. 第二種分頁瀏覽數(shù)據(jù)記錄的完整源程序代碼(no2.aspx): no2.aspx和no1.aspx在程序設(shè)計(jì)的思想和方法上大致相同,下面是no2.aspx的源程序,具體如下: <% @ Page Language = "VB" %>
<% @ Import Namespace = "System.Data" %>
<% @ Import Namespace = "System.Data.OleDb" %>
<script runat = "server" >
Const Record_Per_Page As Short = 5 '定義每一頁顯示的記錄數(shù)
Private Script_Name As String
Sub Page_Load ( Source As Object , e As EventArgs )
Script_Name = GetPageName ( )
'第二種方式來分頁顯示數(shù)據(jù)
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 '定義數(shù)據(jù)連接字符串
Dim SQL As String '定義SQL語句
Dim odConn As OleDbConnection
Dim odAdapt As OleDbDataAdapter
Dim DS As DataSet '創(chuàng)建DataSet對(duì)象
Dim DT As DataTable '創(chuàng)建DataTable對(duì)象
Dim nRecCount As Integer '保存記錄總數(shù)
Dim nPageCount As Integer '保存總共的數(shù)據(jù)頁面數(shù)目
Dim nPage As Integer '存放要瀏覽當(dāng)前數(shù)據(jù)頁面號(hào)
Dim nStart As Integer '存放當(dāng)前頁面的起始記錄序號(hào)
Dim nEnd As Integer '存放當(dāng)前頁面的終止記錄序號(hào)
Dim nPageEnd As Integer '存儲(chǔ)當(dāng)前頁面的最后一面的序號(hào)
Dim i As Integer
'確認(rèn)要瀏覽的頁面序號(hào)
nPage = Convert.ToInt32 ( Request.QueryString ( "Page" ) )
SQL = "SELECT * FROM tblItem "
'創(chuàng)建數(shù)據(jù)連接字符串
strConn = " Provider = Microsoft.Jet.OLEDB.4.0 ; " & _
" Data Source = " & Server.MapPath ( "data.mdb" ) & " ; " & _
" User ID = ; Password = ; "
Try
'得到數(shù)據(jù)記錄總數(shù)
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("錯(cuò)誤信息: <b >" & e.Message & "</b > <p > " )
nRecCount = 0
End Try
If nRecCount > 0 Then
' 確定數(shù)據(jù)記錄要顯示的頁面數(shù)
nPageCount = nRecCount Record_Per_Page
If nRecCount Mod Record_Per_Page > 0 Then
nPageCount += 1
End If
'確認(rèn)瀏覽命令中的頁面參數(shù)是否越界,如果越界則重置頁面序號(hào)
If nPage < 1 Then
nPage = 1
End If
If nPage > nPageCount Then
nPage = nPageCount
End If
Response.Write ( "總共有數(shù)據(jù)記錄" & nRecCount.ToString ( ) & " 條" & "。<br >" )
Response.Write(" <p > <b >第二種分頁顯示為:</b > <p > " )
'確認(rèn)當(dāng)前頁面的開始記錄和終止記錄
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 > 數(shù)據(jù)導(dǎo)航: " )
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 > 本文介紹的這二種分頁瀏覽記錄類型雖然采用的數(shù)據(jù)庫都是本地?cái)?shù)據(jù)庫,但對(duì)其他類型的數(shù)據(jù)庫也是一樣適用的,這只需要修改一下數(shù)據(jù)連接字符串就可以實(shí)現(xiàn)了,譬如如果采用了SQL Server數(shù)據(jù)庫。此SQL Server數(shù)據(jù)庫服務(wù)器是"Server1",數(shù)據(jù)庫是"Data",用戶名為缺省的"sa",沒有設(shè)定密碼。只需要把上面二段程序中的字符串"strConn"變換成: strConn = "Provider = SQLOLEDB.1 ; Persist Security Info = False ; User ID = sa ; Initial Catalog = Data ; Data Source = server1 " 就可以實(shí)現(xiàn)了。 六. 總結(jié): 本文介紹的二種分頁瀏覽數(shù)據(jù)記錄方法在ASP.NET數(shù)據(jù)庫編程方面是非常有用的,因?yàn)樵跀?shù)據(jù)處理方面,分頁顯示記錄比起其他的一些處理,譬如:數(shù)據(jù)修改、刪除等都要難些。希望上面的這些內(nèi)容對(duì)你利用ASP.NET開發(fā)數(shù)據(jù)庫程序有所幫助。(出處:賽迪網(wǎng))