DATALIST分页

来源:互联网 发布:北京移动网络优化中心 编辑:程序博客网 时间:2024/06/03 02:34

<%@ Import NameSpace="System.Data" %>
<%@ Import NameSpace="System.Data.OleDb" %>
<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 = "15"
      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("data/data.mdb")
      Dim objConn As New OleDbConnection(CnString)
      Dim objDA As New OleDbDataAdapter("SELECT * FROM chanpin ORDER BY cpid 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), "chanpin")

      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="425" cellSpacing="0" cellPadding="0" width="204" border="0" ms_2d_layout="TRUE">
   <TR vAlign="top">
    <TD width="204" height="425">
     <form id="Form1" runat="server" method="post">
      <TABLE height="187" cellSpacing="0" cellPadding="0" width="413" border="0" ms_2d_layout="TRUE">
       <TR vAlign="top">
        <TD width="1" height="15"></TD>
        <TD width="412"></TD>
       </TR>
       <TR vAlign="top">
        <TD height="172"></TD>
        <TD>
         <TABLE height="171" cellSpacing="0" cellPadding="0" border="0" width="411">
          <TR vAlign="top">
           <TD height="19"></TD>
           <TD>
            <a name="this"></a>
           </TD>
           <TD rowSpan="2">
            <table align="right" height="25">
             <tr>
              <td align="left">
               <asp:label ID="lblStatus" Runat="server" Font-Name="verdana" Font-Size="10pt" />
              </td>
              <td>
               <a href="datalistpaging.aspx#this" ID="hrefFirst" onserverclick="ShowFirst" runat="server">
                <b>第一页</b></a>
              </td>
              <td>
               <a href="datalistpaging.aspx#this" ID="hrefPrevious" onserverclick="ShowPrevious" runat="server">
                <b>上一页</b></a>
              </td>
              <td>
               <a href="datalistpaging.aspx#this" ID="hrefNext" onserverclick="ShowNext" runat="server">
                <b>下一页</b></a>
              </td>
              <td>
               <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="31"></TD>
           <TD colSpan="2">
            <asp:DataList ID="dList" Runat="server" Width="100%" ItemStyle-BackColor="Beige" ItemStyle-Font-Name="宋体"
             BorderWidth="1" HeaderStyle-Font-Name="Verdana" EnableViewState="False" CellPadding="0" HorizontalAlign="Center"
             RepeatDirection="Horizontal" RepeatColumns="15">
             <HeaderTemplate>
              <table width="100%" style="font: 10pt verdana" cellpadding="0" cellspacing="0">
               <tr style="background-color:FF0000">
                <th align="left">
                 <font color="#FFFFFF">编号</font></th>
                <th align="left">
                 <font color="#FFFFFF">大类</font></th>
                <th align="left">
                 <font color="#FFFFFF">小类</font></th>
                <th align="left">
                 <font color="#FFFFFF">价格</font></th>
                <th align="left">
                 <font color="#FFFFFF">图片</font></th>
               </tr>
             </HeaderTemplate>
             <ItemTemplate>
              <tr style="background-color:#f5f5dc">
               <td><%# DataBinder.Eval(Container.DataItem, "cpid") %></td>
               <td><%# DataBinder.Eval(Container.DataItem, "dalei") %></td>
               <td><%# DataBinder.Eval(Container.DataItem, "xiaolei") %></td>
               <td><%# DataBinder.Eval(Container.DataItem, "jiage") %></td>
               <td><%# DataBinder.Eval(Container.DataItem, "tupian") %></td>
              </tr>
             </ItemTemplate>
             <FooterTemplate>
             </FooterTemplate>
            </asp:DataList></TD>
          </TR>
          <TR vAlign="top">
           <TD height="20"></TD>
           <TD colSpan="2">
            <asp:label ID="intCurrIndex" Visible="False" Runat="server" /></TD>
          </TR>
          <tr>
           <td colspan="2"><asp:label ID="intRecordCount" Visible="False" Runat="server" /></td>
          </tr>
         </TABLE>
        </TD>
       </TR>
      </TABLE>
     </form>
    </TD>
   </TR>
  </TABLE>
 </body>
</HTML>