基於組件的asp編程之二--分頁對象
在asp中,分頁使用的最多的程式段了,把分頁寫成函數,調用起來,要傳很多參數,寫成對象,可以使分頁
對象調用簡單,本文在參考眾多網友的分頁函數基礎上,用javascript把它寫成了一個對像放於檔案
夾"_ScriptCom"下,檔案名稱為"JPageNavbar.asp",先看一下分頁對象的調用方式(由於大多數的asp開發人員使
用vbscript,所以本文的執行個體採用vbscript編寫):
<%@LANGUAGE="VBSCRIPT" %>
<!--#include file="../connections/dbconn.asp" -->
<!--#include file="../_ScriptCom/FAdodb.asp" -->
<!--#include file="../_ScriptCom/FSession.asp" -->
<!--#include file="../_ScriptCom/JPageNavbar.asp" -->
<%
Response.Buffer=true
on error resume next
if trim(Request.ServerVariables("REQUEST_METHOD")) = "POST" then
'取得查詢字串
chxstr=readForm("chxstr")
xshstr=readForm("xshstr")
r1=readForm("R1")
'儲存查詢條件
session("r1")=r1
session("chxstr")=chxstr
session("xshstr")=xshstr
else
如果不是從form提交,則是分頁,從session取的查詢條件
r1=readSession("r1")
chxstr=readSession("chxstr")
xshstr =readSession("xshstr")
end if
'這裡是一些關於業務的邏輯運算
if xshstr=empty then
xshstr ="查詢所有記錄"
end if
select case r1
case 1 '在校學生
sql=" select * from v_student_base where graduate=0"
if chxstr<>empty then
sql=sql+" and "+ chxstr
end if
cddr="在校學生"
case 2 ' 畢業學生
sql=" select * from v_student_base where graduate=1"
if chxstr<>empty then
sql=sql+" and "+ chxstr
end if
cddr="畢業學生"
case 0 '全部學生
if chxstr<>empty then
sql="select * from v_student_base where"+" "+chxstr
else
sql="select * from v_student_base"
end if
cddr="全部學生"
case else
response.write "系統參數錯誤,請與系統管理員聯絡!"
response.End
end select
'產生connection 和 Recordset
set conn=connCreate(getDBLink())
set rs=rsCreate()
rs.open sql,conn,1,3
if (rs.eof ) then
show_msg "很遺憾,沒有您要的記錄!",4,"infoQuery.asp"
end if
dim gd(1)
gd(0)="未畢業"
gd(1)="已畢業"
'***************************************************************************
'注意:這裡是分頁
RowCount =15
set fy=createJPageNavbar()
if (not isEmpty(rs)) then
rs.PageSize = RowCount '設定資料集的頁記錄
fy.PageSize=RowCount
rs.AbsolutePage =fy.getCurrentPage()
fy.RecordCount=rs.RecordCount
fy.PageCount=rs.pageCount
fy.PnWidth="100%"
fy.PnAlign="center"
fy.PlWidth="100%" '表格寬度
fy.PlAlign="right" ' 表格的對齊
end if
'***********************************************************************************
%>
<HTML>
<HEAD>
<TITLE>學生資訊查詢</TITLE>
<link href="../css/style.css" rel="stylesheet" type="text/css">
</HEAD>
<BODY text="#000000" bgColor="#ffffff" leftMargin="0" topMargin="0">
<br>
<TABLE width="550" border="1" cellspacing="0" cellpadding="1" align="center" class="t_table"
ID="Table1">
<TR>
<TD colspan="2" class="t_head"> :::學生資訊搜尋結果::: </TD>
</TR>
<TR>
<TD width="80%" align="left"><B>==>>查詢條件:</B> <%=xshstr %></TD>
<TD width="20%" align="right"><INPUT type="button" value="返回" class="button"
onclick="gofind()" ID="Button1" NAME="Button1"> </TD>
</TR>
<TR>
<TD colspan="2"><table width="100%" border="0" cellpadding="1" cellspacing="0"
class="t_table" ID="Table2">
<tr align="center">
<td colspan="7"><%
'***************************************************
fy.pnDisplay() '分頁的“上一頁” “下一頁”
'***********************************************************
%></td>
</tr>
<tr>
<td width="13%" class="t_head">學號</td>
<td width="11%" class="t_head">姓名</td>
<td width="8%" class="t_head">性別</td>
<td width="10%" class="t_head">年級</td>
<td width="20%" class="t_head">專業</td>
<td width="28%" class="t_head">二級學院</td>
<td width="10%" class="t_head">狀態</td>
</tr>
<% i=0
'********************************************************
while (not rs.eof and i<RowCount) '控制一頁顯示的記錄條數
'*********************************************************
%>
<tr>
<td width="13%" align="center"><a href="#" onclick="openWindow2('stu_msg.asp?
stu_num=<%= rs("stu_num")%>')"><%= rs("stu_num") %></a></td>
<td width="11%" align="center"><%= rs("name") %></td>
<td width="8%" align="center"><%= rs("sex") %></td>
<td width="10%" align="center"><%= rs("gread") %></td>
<td width="20%" align="center"><%= rs("speciality_name") %></td>
<td width="28%" align="center"><%= rs("secondary") %></td>
<td width="10%" align="center"><%=gd(rs("graduate")) %></td>
</tr>
<% i=i+1
rs.moveNext
wend %>
<tr align="right">
<td colspan="7"><%
'**************************************************************
fy.plDisplay() '分頁列表
'**************************************************************
%></td>
</tr>
</table> </TD>
</TR>
</TABLE>
</BODY>
</HTML>
<%
rsNull(rs)
connNull(conn)
%>
這裡是分頁顯示的(做了一下處理)
這裡是原始碼
<SCRIPT LANGUAGE=javascript RUNAT=Server>
// ************************************************************************
// Script Compont Object Model
// Design for Active Server Pages
//
// Copyright 2003 Version 1.0
// Made by newsunet
// 請不要刪除這一段注釋,自由傳播,保留所有權
// ************************************************************************
/*//Ado.RecordSet記錄分頁對象
//設定分頁
var RowCount =3
var fy=new JPageNavbar()
if (!rsRpt.Eof){
rs.PageSize = RowCount //設定資料集的頁記錄
fy.PageSize=RowCount
rs.AbsolutePage =fy.getCurrentPage()
fy.RecordCount=rs.RecordCount
fy.PageCount=rs.pageCount
fy.PnWidth="100%"
fy.PnAlign="right"
fy.PlWidth="100%" //表格寬度
fy.PlAlign="right" // 表格的對齊
}
//顯示分頁
<%fy.pnDisplay()%>
<%fy.plDisplay()%>
*/
function createJPageNavbar(){
//這個函數是vbscript的介面函數 ,vbscript不是基於對象的指令碼語言
var objJPageNavbar=new JPageNavbar
return objJPageNavbar
}
function JPageNavbar(){
// public members
this.PageSize="0"
this.RecordCount="0" //總記錄數
this.PageCount="1" //總頁數
this.CurrentPage="1"
this.PnWidth="100%"
this.PnAlign="right"
this.PlWidth="100%" //表格寬度
this.PlAlign="right" // 表格的對齊
// private members
//public methods
this.getCurrentPage=_getCurrentPage
this.pnDisplay = _PN_show;
this.plDisplay = _PL_show;
//private methods
}
function _getCurrentPage(){
//當前顯示的是第幾頁
//取得當前頁
var pageNo = Request.QueryString ("PageNo")
//如果沒有選擇第幾頁,則預設顯示第一頁;
if (isNaN(pageNo)) {
pageNo = 1
}
this.CurrentPage=pageNo
return pageNo
}
function _PL_show(){
var strBuilder=new String()
strBuilder=""
var p=(this.CurrentPage-(this.CurrentPage%10))/10 //計算分頁顯示的頁數
//首組為第0族
strBuilder+="<table border=/"0/" cellpadding=/"0/" cellspacing=/"0/" "
strBuilder+=" width=/""+this.PlWidth+"/" align=/""+this.PlAlign+"/">"
strBuilder+=" <tr> "
strBuilder+=" <td valign=/"middle/" align=/"right/">分頁:"
if (this.CurrentPage==1){
strBuilder+="<font face=/"webdings/" color=/"#ff0000/">9</font> "
}
else{
strBuilder+="<a href=/"?PageNo=1/" title=/"首頁/"><font face=/"webdings/">9</font></a> "
}
//上十頁
if (p*10>0){
strBuilder+="<a href=/"?PageNo="+(p*10)+"/" title=上十頁><font
face=/"webdings/">7</font></a> "
}
strBuilder+="<b>"
//分頁列表
for(var i=p*10+1;i<=p*10+10;i++){
if (i==this.CurrentPage){
strBuilder+="<font color=/"#000000/">"+i+"</font> "
}
else{
strBuilder+="<a href=?PageNo="+i+" title=/"轉到: 第"+i+"頁/">"+i+"</a> "
}
if (i>=this.PageCount) break;
}
strBuilder+= "</b>"
//顯示下十頁
if (i<this.PageCount){
strBuilder+="<a href=/"?PageNo="+i+"/" title=/"下十頁/"><font
face=/"webdings/">8</font></a> "
}
//顯示尾頁
if (this.CurrentPage==this.PageCount){
strBuilder+= "<font face=/"webdings/" color=/"#000000/">:</font> "
}
else{
strBuilder+= "<a href=?PageNo="+this.PageCount+" title=/"尾頁/"><font
face=/"webdings/">:</font></a> "
}
strBuilder+= "</td></tr></table>"
Response.Write(strBuilder)
}
function _PN_show(){
var strBuilder=new String()
var nextPageNo
strBuilder=""
strBuilder+="<table border=/"0/" cellpadding=/"0/" cellspacing=/"0/" "
strBuilder+=" width=/""+this.PnWidth+"/" align=/""+this.PnAlign+"/">"
strBuilder+="<tr>"
strBuilder+="<td valign=/"middle/">頁次:
[<b>"+this.CurrentPage+"</b>/<b>"+this.PageCount+"</b>]頁 每頁[<b>"+this.PageSize+"</b>]條 總記錄
數:[<b>"+this.RecordCount+"</b>]條</td>"
strBuilder+="<td align=/"right/">"
if (this.CurrentPage>1){
nextPageNo=this.CurrentPage
nextPageNo--
strBuilder+="[<a href=?pageNo="+nextPageNo+" title=/"轉到上一頁/">上一頁</a>]"
}
if (this.CurrentPage<this.PageCount){
nextPageNo=this.CurrentPage
nextPageNo++
strBuilder+="[<a href=?pageNo="+nextPageNo+" title=/"轉到下一頁/">下一頁</a>]"
}
strBuilder+="</td></tr></table>"
Response.Write(strBuilder)
}
</SCRIPT>
本文原名:基於組件的asp編程之二--分頁對象