ASP.NET處理數據分頁(1)
2024-07-10 12:55:37
供稿:網友
在asp的數據庫編程的時,由于瀏覽器的大小限制,在要瀏覽的數據記錄比較多的時候,為了達到更直觀的效果,我們把這些數據記錄分成若干的頁面,通過數據導航按鈕(或者其他超鏈接),分頁的瀏覽。其實這種數據記錄的分頁瀏覽在asp.net也能夠實現。并且在實現的過程中比起在asp的處理過程顯得條理更清晰,也更容易些。
通過瀏覽器進行分頁瀏覽數據記錄基本類型主要有二種。其他類型的分頁瀏覽要么是對這二種類型的修改,要么是對這二種類型的綜合。具體表現方式的如下面這二幅圖:
下面就來探討一下在asp.net中這二種分頁瀏覽數據記錄的具體實現過程:
首先來介紹一下我們使用的數據庫,在本文中為了方便起見,我們使用了本地數據庫access 2000,數據庫名稱為'data.mdb',里面存放了一張數據表'tblitem'。此數據表的結構如下:
字段名稱 字段類型
itemid 自動編號
itemname 文本類型
如果你采用的是別的數據庫,只需對下面介紹的程序進行簡單的修改就可以了。這將在下面介紹。
一. 本文程序設計和運行的軟件環境:
(1).微軟公司視窗2000服務器版
(2)..net framework sdk beta 2
二. 第一種分頁瀏覽數據記錄的關鍵步驟以及實現方法:
(1).首先要得到初始瀏覽數據記錄的超鏈接字符串:
這其實很關鍵,因為在第一種分頁瀏覽中的'首頁'、'下一頁'等操作,都是通過在這個超鏈接字符串后面加入要瀏覽頁面的參數來實現的,在本文的程序中是通過getpagename ( )函數來實現的。此函數具體如下:
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).要得到你所要瀏覽的數據記錄總數:
在本文中,為了方便,我們是把數據表'tblitem'中的全部記錄都拿來瀏覽。asp.net頁面通過ado.net來得到數據表'tblitem'。下面代碼就是利用ado.net來得到'tblitm'表中記錄總數的程序代碼:
<% @ page language = 'vb' %>
<% @ import namespace = 'system.data' %>
<% @ import namespace = 'system.data.oledb' %>
<script runat = 'server' >
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 nstart as integer '存放當前頁面的起始記錄序號
dim nend 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
</script >
(3).計算出瀏覽的數據記錄總共頁面數:
在瀏覽頁面中,我們發現了每一頁只瀏覽5條記錄,你可以通過修改程序中定義一個常量'record_per_page'來改變每一頁瀏覽數據記錄的個數。在知道了要瀏覽數據記錄的總數后,通過下面代碼來計算出要顯示這些數據記錄所需要的頁面總數:
const record_per_page as short = 5 '定義每一頁顯示的記錄數
dim npagecount as integer '保存總共的數據頁面數目
dim npage as integer '存放要瀏覽當前數據頁面號
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
(4).數據導航的實現方法:
其實數據導航是通過對參數'page'賦值來實現的,其中程序中的'npage'是當前數據頁面序號, 'npagecount'是數據頁面的總和。下面是實現這些數據導航的具體實現代碼:
response.write ( ' <p >數據導航:<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 > ' )
'顯示當前頁和合計頁數
response.write ( ' ' & '頁次:'& npage.tostring ( ) & '/' & npagecount.tostring ( ) & ' <br > ' )
(5).顯示不同頁面的數據記錄:
根據超鏈接字符串得到'page'值,然后根據此值來得到在此頁面中要顯示的起始記錄號和結束記錄號,再通過一個循環把這些記錄給顯示出來。下面這行代碼是讀取參數'page':
npage = convert.toint32 ( request.querystring ( 'page' ) )
下面這些代碼是根據用戶想要去的頁面得到起始記錄號和結尾記錄號,并通過屏幕顯示出來:
dim nstart as integer '存放當前頁面的起始記錄序號
dim nend as integer '存放當前頁面的終止記錄序號
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
在本文介紹的二種分頁瀏覽記錄類型中,對于數據顯示,我們采用了簡化處理。我們知道在瀏覽器上,瀏覽記錄一般都是通過dbgrid的形式來實現的,這一點在其實也好實現,讀者只需稍微修改一下本文中的程序代碼就可以實現。
三. 第一種分頁瀏覽數據記錄的完整程序代碼(no1.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 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 >數據導航:<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 > ' )
'顯示當前頁和合計頁數
response.write ( ' ' & '頁次:'& npage.tostring ( ) & '/' & npagecount.tostring ( ) & ' <br > ' )
end sub
</script >