asp+ajax实现分页效果

建一个文件list.htm

<html>
<head>
<title>新闻列表简化型</title>
<scriptlanguage="javascript">
<!--
//ajax控件也可以做成单独文件方便到其它地方调用
functionAjax()
{
varxhrObj=null;
if(window.XMLHttpRequest)
{
xhrObj=newXMLHttpRequest();
}
elseif(window.ActiveXObject)
{
try{
xhrObj=newActiveXObject("Microsoft.XMLHTTP");
}
catch(e1)
{
try{
xhrObj=newActiveXObject("MSXML2.XMLHTTP");
}
catch(e2){
try{
xhrObj=newActiveXObject("MSXML3.XMLHTTP");
}
catch(e3){
alert("创建Ajax失败:"+e3)
}
}
}
}
else
{
alert("未能识别的浏览器");
}
returnxhrObj;
}

function$(id)
{
returndocument.all[id];
}

</script>
<scriptlanguage="javascript"defer>
varxhr=newAjax();
varpage=0;
varicount=0;//当前页数
varpagecount;//总页数
varcount=0;
varipagecount=0;
varipagesize=5;;//每页显示记录数
varpp=0;
varipage=0;
varDom=null;

functiongetList(curPage)
{
if(ipagecount!=0)
{
ipage=parseInt((curPage-1)/ipagecount)+1;
}else{
ipage=1
};
page=curPage;
if(pp!=ipage)
{;
Dom=null;
pp=ipage;
xhr.open("get","data.asp?curpage="+pp,true);
xhr.onreadystatechange=$CallBack;
xhr.send(null);
}
else
{
$showList();
}
}
function$CallBack()
{
if(xhr.readystate==1)
{
$("newsList").innerHTML="正在连结服务器";
}
elseif(xhr.readystate==2||xhr.readystate==3)
{
$("newsList").innerHTML="正在读取数据";
}
elseif(xhr.readystate==4)
{
if(xhr.status==200)
{
Dom=xhr.responseXML;
$showList();
}
}
}


function$showList()
{
varhtml="";
varitems=Dom.selectNodes("//item");
icount=Dom.selectNodes("//item").length;//数量
count=Dom.selectSingleNode("//data").getAttribute("count");//总数量
ipagecount=parseInt((icount-1)/ipagesize)+1;
pagecount=parseInt((count-1)/ipagesize)+1;
startPosition=(page-1)*ipagesize;
endPosition=(page*ipagesize)
if(endPosition>count)endPosition=count
for(vari=startPosition;i<endPosition;i++)
{
html+="<divid=\"title\"><ahref=\""+items[i].selectSingleNode("htmlurl").text+"\">"+items[i].selectSingleNode("hits").text+"</a></div>";
}
$("newsList").innerHTML=html;

varstatushtml="<ahref=\"javascript:previousPage()\">上一页</a>|<ahref=\"javascript:nextPage()\">下一页</a>";
if(pagecount<=10)
{
for(vari=1;i<=pagecount;i++)
{
if(i==page)
{
statushtml+="<b>"+i+"</b>&nbsp;"
}
else
{
statushtml+="<ahref=\"javascript:turnPage("+i+")\">"+i+"</a>&nbsp;"
}
}
}
elseif(page+10<pagecount&&pagecount>10)
{
if(page%10==0)
{
for(vari=page-9;i<=page+1;i++)
{
if(i==page)
{
statushtml+="<b>"+i+"</b>&nbsp;"
}
else
{
statushtml+="<ahref=\"javascript:turnPage("+i+")\">"+i+"</a>&nbsp;"
}
}
}
elseif(page%10==1&&page!=1)
{
for(vari=page-1;i<=page+10;i++)
{
if(i==page)
{
statushtml+="<b>"+i+"</b>&nbsp;"
}
else
{
statushtml+="<ahref=\"javascript:turnPage("+i+")\">"+i+"</a>&nbsp;"
}
}
}
elseif(page<=11)
{
for(vari=1;i<=11;i++)
{
if(i==page)
{
statushtml+="<b>"+i+"</b>&nbsp;"
}
else
{
statushtml+="<ahref=\"javascript:turnPage("+i+")\">"+i+"</a>&nbsp;"
}
}
}
else
{
for(vari=(page-(page%10));i<=page+(10-(page%10))+1;i++)
{
if(i==page)
{
statushtml+="<b>"+i+"</b>&nbsp;"
}
else
{
statushtml+="<ahref=\"javascript:turnPage("+i+")\">"+i+"</a>&nbsp;"
}
}
}
}
elseif(page+10>=pagecount&&pagecount>10)
{
if(page%10==0)
{
for(vari=page-9;i<=page+1;i++)
{
if(i==page)
{
statushtml+="<b>"+i+"</b>&nbsp;"
}
else
{
statushtml+="<ahref=\"javascript:turnPage("+i+")\">"+i+"</a>&nbsp;"
}
}
}
elseif(page%10==1&&page!=1)
{
for(vari=page-1;i<=page+9;i++)
{
if(i==page)
{
statushtml+="<b>"+i+"</b>&nbsp;"
}
else
{
statushtml+="<ahref=\"javascript:turnPage("+i+")\">"+i+"</a>&nbsp;"
}
}
}
}
document.getElementById("pagestatus").innerHTML=statushtml;

}

functionpreviousPage()
{
if(page>1)
{
getList(page-1);
}
else
{
alert("已经是第一页了");
}

}

functionnextPage()
{
if(page<ipagecount)
{
getList(page+1);
}
else
{
alert("已经到最后一页了");
}
}

functionturnPage(p)
{
getList(p);
}

getList(1);
</script>
</head>
<body>
<divid="newsList"></div>
<divid="pagestatus"></div>
</body>
</html>

建一个文件data.asp
注:这里我用的是ACCESS数据库,库名叫data.mdb,当然这个自己可以定义,库中表名为info字段分别是id(自动编号)、a、b、c、d(日期类型)

<%@Language="VBSCRIPT"codepage="936"%>
<%
response.cachecontrol="no-cache"
response.addHeader"pragma","no-cache"
response.expires=-1
response.expiresAbsolute=now-1
response.contentType="text/xml"
'SQL数据库Setconn=server.CreateObject("adodb.connection")
'sconn="driver={sqlserver};server=(local);uid=**;pwd=**;database=***"
'conn.opensconn
setconn=server.CreateObject("adodb.connection")
file=server.mappath("data.mdb")
conn.Open"driver={microsoftaccessdriver(*.mdb)};"&"dbq="&file%>
<?xmlversion="1.0"encoding="gb2312"?>
<%
dimiPagesize,rs,sSQL,iCurPage,ipresize,icount,ipagecount

icurpage=trim(request.QueryString("curpage"))

iPagesize=500'页大小
setrs=server.createObject("adodb.recordset")
'rs.open"selectcount(id)fromca_newswherestatus=1",conn,1,1
rs.open"selectcount(id)frominfo",conn,1,1
icount=rs(0)'总记录数
rs.close

ipagecount=int((icount-1)/iPagesize)+1'总页数
ificurpage=""then
icurpage=1
else
icurpage=cint(icurpage)
endif
ificurpage<1then
icurpage=1
endif

ificurPage>ipagecountthenicurpage=ipagecount
ipreSize=(iCurPage-1)*ipagesize
ifipresize=0then
sSQL="select*frominfoorderbyiddesc"
else
'sSQL="selecttop"&ipagesize&"id,title,postdate,hits,htmlurlfromnewswherestatus=1andidnotin(selecttop"&ipresize&"idfromnewswherestatus=1orderbyidasc)orderbyidasc"
sSQL="selecttop"&ipagesize&"*frominfoidnotin(selecttop"&ipresize&"idfrominfoorderbyiddesc)orderbyiddesc"
endif
rs.opensSQL,conn,1,1

%>
<datacount="<%=icount%>"pagesize="<%=ipagesize%>"pagecount="<%=ipagecount%>">
<%
whilenotrs.eof
%>
<itemid="<%=rs("id")%>">
<title>&lt;![CDATA[<%=rs("a")%>]]&gt;</title>
<htmlurl>&lt;![CDATA[<%=rs("b")%>]]&gt;</htmlurl>
<postdate><%=DateValue(rs("d"))%></postdate>
<hits><%=trim(rs("c"))%></hits>
</item>
<%
rs.movenext
wend
%>
</data>
<%
rs.close
setrs=nothing
conn.close
setconn=nothing
%>

你可能感兴趣的:(JavaScript,Ajax,SQL Server,VBScript,asp)