当前位置:首页 > DataList的分页技术

DataList的分页技术

点击次数:956  更新日期:2010-12-27
\n

由于DataList不支持内建的分页机制,因此,我们必须使用 SqlDataAdapter对象的Fill方法来实现分页,Fill方法主要用来增加或刷新DataSet的记录行。Fill方法已经被重载,我们这里选用四个参数的那个重载方法:DataSet, startRecord, maxRecords 和 TableName。


\n

复制ASPX代码保存代码<%@ 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


\n

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()


\n

If Not Page.IsPostBack() Then
objDA.Fill(objDS)
intRecordCount.Text = CStr(objDS.Tables(0).Rows.Count)
objDS = Nothing
objDS = New DataSet()
End If


\n

objDA.Fill (objDS, CInt(intCurrIndex.Text), CInt(intPageSize.Text), “Document”)


\n

dList.DataSource = objDS.Tables(0).DefaultView
dList.DataBind()
objConn.Close()
PrintStatus()
End Sub


\n

Public Sub ShowFirst(ByVal s As Object, ByVal e As EventArgs)
intCurrIndex.Text = “0″
DataBind()
End Sub


\n


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


\n

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


\n

Public Sub ShowLast(ByVal s As Object, ByVal e As EventArgs)
Dim tmpInt as Integer


\n

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


\n

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>”


\n

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>


\n

转自:孟宪会之精彩世界

\n