DataList的分页技术
2024-07-21 02:23:08
供稿:网友
由于datalist不支持内建的分页机制,因此,我们必须使用 sqldataadapter对象的fill方法来实现分页,fill方法主要用来增加或刷新dataset的记录行。fill方法已经被重载,我们这里选用四个参数的那个重载方法:dataset, startrecord, maxrecords 和 tablename。
<%@ import namespace="system.data.oledb" %>
<%@ import namespace="system.data" %>
<html>
<head>
<script language="vb" runat="server">
private sub page_load(byval sender as system.object, byval e as system.eventargs)
if not page.ispostback() then
intpagesize.text = "2"
intcurrindex.text = "0"
databind()
end if
end sub
private sub databind()
dim cnstring as string
cnstring = "provider=microsoft.jet.oledb.4.0;data source="
cnstring = cnstring + server.mappath("datadb.mdb"))
dim objconn as new oledbconnection(cnstring)
dim objda as new oledbdataadapter("select * from document order by createdate desc", objconn)
dim objds as new dataset()
if not page.ispostback() then
objda.fill(objds)
intrecordcount.text = cstr(objds.tables(0).rows.count)
objds = nothing
objds = new dataset()
end if
objda.fill (objds, cint(intcurrindex.text), cint(intpagesize.text), "document")
dlist.datasource = objds.tables(0).defaultview
dlist.databind()
objconn.close()
printstatus()
end sub
public sub showfirst(byval s as object, byval e as eventargs)
intcurrindex.text = "0"
databind()
end sub
public sub showprevious(byval s as object, byval e as eventargs)
intcurrindex.text = cstr(cint(intcurrindex.text) - cint(intpagesize.text))
if cint(intcurrindex.text) < 0 then
intcurrindex.text = "0"
end if
databind()
end sub
public sub shownext(byval s as object, byval e as eventargs)
if cint(intcurrindex.text) + 1 < cint(intrecordcount.text) then
intcurrindex.text = cstr(cint(intcurrindex.text) + cint(intpagesize.text))
end if
databind()
end sub
public sub showlast(byval s as object, byval e as eventargs)
dim tmpint as integer
tmpint = cint(intrecordcount.text) mod cint(intpagesize.text)
if tmpint > 0 then
intcurrindex.text = cstr(cint(intrecordcount.text) - tmpint)
else
intcurrindex.text = cstr(cint(intrecordcount.text) - cint(intpagesize.text))
end if
databind()
end sub
private sub printstatus()
lblstatus.text = "总记录数:<b>" & intrecordcount.text
lblstatus.text += "</b> 当前:<b> "
lblstatus.text += cstr(cint(cint(intcurrindex.text) / cint(intpagesize.text)+1))
lblstatus.text += "</b>/<b>"
if (cint(intrecordcount.text) mod cint(intpagesize.text)) > 0 then
lblstatus.text += cstr(cint(cint(intrecordcount.text) / cint(intpagesize.text)+1))
else
lblstatus.text += cstr(cint(intrecordcount.text) / cint(intpagesize.text))
end if
lblstatus.text += "</b>"
end sub
</script>
</head>
<body ms_positioning="gridlayout">
<table height="528" cellspacing="0" cellpadding="0" width="244" border="0"
ms_2d_layout="true">
<tbody>
<tr valign="top">
<td width="244" height="528">
<form id="form1" method="post" runat="server">
<table height="227" cellspacing="0" cellpadding="0" width="516" border="0"
ms_2d_layout="true">
<tr valign="top">
<td width="10" height="15"></td>
<td width="506"></td>
</tr>
<tr valign="top">
<td height="48"></td>
<td>
<h2 align="center"><font face="verdana">paging in datalist</font></h2>
</td>
</tr>
<tr valign="top">
<td height="106"></td>
<td>
<a name="this"></a>
</td>
</tr>
<tr valign="top">
<td height="19"></td>
<td rowspan="2">
<table width="505" align="right" height="25">
<tr>
<td width="76%" align="left">
<asp:label id="lblstatus" runat="server" font-name="verdana" font-size="10pt" />
</td>
<td width="6%">
<a href="datalistpaging.aspx#this" id="hreffirst" onserverclick="showfirst" runat="server">
<b><<</b></a>
</td>
<td width="6%">
<a href="datalistpaging.aspx#this" id="hrefprevious"
onserverclick="showprevious" runat="server">
<b><</b></a>
</td>
<td width="6%">
<a href="datalistpaging.aspx#this" id="hrefnext" onserverclick="shownext" runat="server">
<b>></b></a>
</td>
<td width="6%">
<a href="datalistpaging.aspx#this" id="hreflast" onserverclick="showlast" runat="server">
<b>>></b></a>
</td>
</tr>
</table>
</td>
</tr>
<tr valign="top">
<td height="19"></td>
<td>
<asp:label id="intpagesize" visible="false" runat="server" /></td>
</tr>
<tr valign="top">
<td height="20"></td>
<td>
<asp:label id="intrecordcount" visible="false" runat="server" /></td>
</tr>
<asp:datalist id="dlist" runat="server" width="100%"
itemstyle-backcolor="beige" itemstyle-font-name="宋体"
borderwidth="1" headerstyle-font-name="verdana" enableviewstate="false">
<headertemplate>
<table width="100%" style="font: 10pt verdana" cellpadding="0" cellspacing="0">
<tr style="background-color:ff0000">
<th align="left">
<font color="#ffffff">store id</font></th>
<th align="left">
<font color="#ffffff">order number</font></th>
<th align="left">
<font color="#ffffff">order date</font></th>
<th align="left">
<font color="#ffffff">qty</font></th>
<th align="left">
<font color="#ffffff">title id</font></th>
</tr>
</headertemplate>
<itemtemplate>
<tr style="background-color:#f5f5dc">
<td><%# databinder.eval(container.dataitem, "id") %></td>
<td><%# databinder.eval(container.dataitem, "title") %></td>
<td><%# databinder.eval(container.dataitem, "author") %></td>
<td><%# databinder.eval(container.dataitem, "source") %></td>
<td><%# databinder.eval(container.dataitem, "createdate") %></td>
</tr>
</itemtemplate>
<footertemplate>
</table>
</footertemplate>
</asp:datalist>
<asp:label id="intcurrindex" visible="false" runat="server" />
</table>
</form>
</td></tr>
</tbody>
</table>
</body>
</html>