ASP.NET處理數(shù)據(jù)分頁(yè)(2)
2024-07-10 12:55:36
供稿:網(wǎng)友
四. 第二種分頁(yè)瀏覽數(shù)據(jù)記錄的關(guān)鍵步驟以及實(shí)現(xiàn)方法: 其實(shí)這二種分頁(yè)方法在程序設(shè)計(jì)中是大同小異的,在第二種方法中,其前面的關(guān)鍵步驟中和第一種分頁(yè)幾乎相同,也是要得到瀏覽數(shù)據(jù)記錄的總數(shù),設(shè)定每一頁(yè)要顯示的數(shù)據(jù)記錄個(gè)數(shù),計(jì)算出總共有多少數(shù)據(jù)頁(yè)面等等。這些步驟的實(shí)現(xiàn)方法可以參考第一種方法。第二種分頁(yè)方法和第一種分頁(yè)方法的主要區(qū)別在于數(shù)據(jù)導(dǎo)航的實(shí)現(xiàn)方法上。下列代碼功能是實(shí)現(xiàn)第二種分頁(yè)方法數(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
五. 第二種分頁(yè)瀏覽數(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 '定義每一頁(yè)顯示的記錄數(shù)
private script_name as string
sub page_load ( source as object , e as eventargs )
script_name = getpagename ( )
'第二種方式來(lái)分頁(yè)顯示數(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語(yǔ)句
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ù)頁(yè)面數(shù)目
dim npage as integer '存放要瀏覽當(dāng)前數(shù)據(jù)頁(yè)面號(hào)
dim nstart as integer '存放當(dāng)前頁(yè)面的起始記錄序號(hào)
dim nend as integer '存放當(dāng)前頁(yè)面的終止記錄序號(hào)
dim npageend as integer '存儲(chǔ)當(dāng)前頁(yè)面的最后一面的序號(hào)
dim i as integer
'確認(rèn)要瀏覽的頁(yè)面序號(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ù)記錄要顯示的頁(yè)面數(shù)
npagecount = nreccount / record_per_page
if nreccount mod record_per_page > 0 then
npagecount += 1
end if
'確認(rèn)瀏覽命令中的頁(yè)面參數(shù)是否越界,如果越界則重置頁(yè)面序號(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 >第二種分頁(yè)顯示為:</b > <p > ' )
'確認(rèn)當(dāng)前頁(yè)面的開(kāi)始記錄和終止記錄
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 >
本文介紹的這二種分頁(yè)瀏覽記錄類(lèi)型雖然采用的數(shù)據(jù)庫(kù)都是本地?cái)?shù)據(jù)庫(kù),但對(duì)其他類(lèi)型的數(shù)據(jù)庫(kù)也是一樣適用的,這只需要修改一下數(shù)據(jù)連接字符串就可以實(shí)現(xiàn)了,譬如如果采用了sql server數(shù)據(jù)庫(kù)。此sql server數(shù)據(jù)庫(kù)服務(wù)器是'server1',數(shù)據(jù)庫(kù)是'data',用戶(hù)名為缺省的'sa',沒(méi)有設(shè)定密碼。只需要把上面二段程序中的字符串'strconn'變換成:
strconn = 'provider = sqloledb.1 ; persist security info = false ; user id = sa ; initial catalog = data ; data source = server1 '
就可以實(shí)現(xiàn)了。
六. 總結(jié):
本文介紹的二種分頁(yè)瀏覽數(shù)據(jù)記錄方法在asp.net數(shù)據(jù)庫(kù)編程方面是非常有用的,因?yàn)樵跀?shù)據(jù)處理方面,分頁(yè)顯示記錄比起其他的一些處理,譬如:數(shù)據(jù)修改、刪除等都要難些。希望上面的這些內(nèi)容對(duì)你利用asp.net開(kāi)發(fā)數(shù)據(jù)庫(kù)程序有所幫助。本文來(lái)源于網(wǎng)頁(yè)設(shè)計(jì)愛(ài)好者web開(kāi)發(fā)社區(qū)http://www.html.org.cn收集整理,歡迎訪問(wèn)。