VB.Net 開發(fā)的長內(nèi)容自動分頁功能
2024-07-10 13:05:37
供稿:網(wǎng)友
 
國內(nèi)最大的酷站演示中心!
長內(nèi)容即可以手動分頁也可以自動分頁。
采用自動分頁功能,只需告訴計算機每頁大約字數(shù)就可以了。
下面附源碼,方便自己,也方便大家研究:
public class webform1
 inherits system.web.ui.page
 protected withevents label1 as system.web.ui.webcontrols.label
 protected withevents label2 as system.web.ui.webcontrols.label
 protected withevents label3 as system.web.ui.webcontrols.label
 protected withevents label4 as system.web.ui.webcontrols.label
#region " web 窗體設計器生成的代碼 "
 '該調(diào)用是 web 窗體設計器所必需的。
 <system.diagnostics.debuggerstepthrough()> private sub initializecomponent()
 end sub
 private sub page_init(byval sender as system.object, byval e as system.eventargs) handles mybase.init
 'codegen: 此方法調(diào)用是 web 窗體設計器所必需的
 '不要使用代碼編輯器修改它。
 initializecomponent()
 end sub
#end region
 private sub page_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
 '在此處放置初始化頁的用戶代碼
 pages()
 end sub
 '長內(nèi)容分頁
 sub pages()
 dim i, start, stops, t, stat, statt, pp, pagecount, pagesize, articleid as integer
 dim pa, articletxt, articletext, contenttext, html as string
 contenttext = "<p>aaaaaaa</p><p>bbbbbbbbbbbb</p><p>cccccccccccccccc</p>"
 '變量初始值
 stat = 0
 statt = 0
 start = 0 '開始查詢的字符串位置,初始為0
 stops = 0
 pagesize = 2 '定義每頁至少顯示字符串數(shù)
 pagecount = 0
 '獲得當前的頁數(shù)
 pa = request.params("page")
 if (pa = "" or isdbnull(pa)) then
 pa = "1"
 end if
 pp = convert.toint32(pa)
 '獲得內(nèi)容
 articletxt = contenttext
 '判斷頁面的內(nèi)容長度是否大于定義的每頁至少顯示字符串數(shù)
 if (articletxt.length >= pagesize) then ' 如果大于字符串數(shù),則我們可以分頁顯示
 t = articletxt.length / pagesize '獲得大致的總頁數(shù)
 '根據(jù)目前獲得的頁數(shù)循環(huán)
 for i = 0 to t
 '如果查詢開始位置到查詢的范圍超出整個內(nèi)容的長度,那么就不用尋找斷點(分頁點);反之,查找
 if (start + pagesize < articletxt.length) then
 stat = articletxt.indexof("</p>", start + pagesize) '查找</p>分頁點的位置
 '如果找不到
 'if (stat <= 0) then
 'stat = articletxt.indexof("</p>", start + pagesize) '查找</p>分頁點的位置;這里您可以自己設置分頁點的判斷
 'end if
 end if
 'response.write("'" & stat & "'")
 if (stat <= 0) then
 '如果找不到分頁點,說明不能分頁,也就不需要做其他的勞動了;否則,就進行分頁
 'articletext = articletxt '將結(jié)果付給要導出的變量
 'label1.text = articletext & stat
 'exit sub
 else
 stops = stat '分頁點的位置也就作為這一頁的終點位置
 if (start + pagesize >= articletxt.length) then '如果起始位置到查詢的范圍超出整個內(nèi)容的長度,那么這一頁的終點位置為內(nèi)容的終點
 stops = articletxt.length
 end if
 if (pp = i + 1) then '如果是當前,那么輸出當前頁的內(nèi)容
 articletext = articletxt.substring(start, stops - start) '取內(nèi)容的起始位置到終點位置這段字符串輸出
 label1.text = articletext
 end if
 start = stat '將終點位置作為下一頁的起始位置
 pagecount = pagecount + 1 '獲得實際頁總數(shù)
 'response.write("-" & pagecount & "-")
 end if
 next
 end if
 '分頁部分(這里就簡單多了)
 '定義分頁代碼變量
 if (pagecount > 1) then '當頁數(shù)大于1的時候我們顯示頁數(shù)
 'response.write(pp)
 if (pp - 1 > 0) then '顯示上一頁,方便瀏覽
 html += "<a href=?id=" & articleid & "&page=" & (pp - 1) & ">[上一頁]</a> "
 else
 if pp = 1 then
 html += "[<font color=#cccccc>上一頁</font>] "
 else
 html += "<a href=?id=" & articleid & "&page=" & (1) & ">[上一頁]</a> "
 end if
 end if
 for i = 1 to pagecount
 if (i = pp) then '如果是當前頁,加粗顯示
 html += "<b>[" & i & "]</b> "
 else
 html += "<a href=?id=" & articleid & "&page=" & i & ">[" & i & "]</a> "
 end if
 next
 if (pp + 1 > pagecount) then '顯示下一頁,方便瀏覽
 if pp = pagecount then
 html += "[<font color=#cccccc>下一頁</font>] "
 else
 html += "<a href=?id=" & articleid & "&page=" & (pagecount) & ">[下一頁]</a></p>"
 end if
 else
 html += "<a href=?id=" & articleid & "&page=" & (pp + 1) & ">[下一頁]</a></p>"
 end if
 end if
 label2.text = html
 end sub
end class