科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航



ZDNet>软件频道>中间件-zhiding>[冷枫]简单的Repeater分页代码

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

简单的Repeater分页代码

来源:CSDN 2007年09月24日

关键字:代码 分页 Repeater 冷枫

practice_1

--------------------------------------------

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script language="vb" runat="server">
 
 '定义bindlist子程序,调用pageno(页面位置)参数
  sub BindList(PageNo As Integer)
  
   '使用connection对象打开数据连接
    Dim ConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                               "Data Source=" & Server.MapPath("db/music.mdb")
   
 '定义sql
 Dim strSQL As String = "Select * From 唱片,风格 where 唱片.风格号码 = 风格.风格号码 Order By '添加时间' Desc"
  
  
    '创建dataadapter对象
 Dim objCmd As New OleDbDataAdapter(strSQL, ConnString)
   
 
 '创建dataset对象
 Dim DS As New DataSet()
   
 '调用dataadapter对象的fill()方法讲选取的数据置入dataset对象。
 objCmd.Fill(DS, "唱片")
  
    '声明mytable 变量为 datatable对象,并将ds.tables("歌曲")赋值给 mytable变量。
    Dim myTable As DataTable = DS.Tables("唱片")   
 
 '定义每页面显示2条记录。
 dim pagesize as integer = 2
 
 '显示留言的总条数
 dim totalrecord as integer = mytable.rows.count
 showtotalrecord.text = totalrecord
 
 '显示流言总页数totalrecord为流言总条数
 dim totalpage as integer
 if totalrecord < pagesize then
   totalpage = 1
 else
   if totalrecord mod pagesize <> 0 then
     totalpage = totalrecord \ pagesize + 1
   else
     totalpage = totalrecord / pagesize
   end if
 end if
  ShowTotalPage.Text = TotalPage
 
  '将当前页码赋给Showpagenumber控件,以显示当前页数
  Showpagenumber.text = pageno
   
    '创建 tempTable 数据表,其结构与mytable相同,tempTable 数据表
    '保存的数据会连接到 Repeater 控件,以显示留言内容。
    Dim tempTable As DataTable = myTable.Clone()
    Dim I, J As Integer
    For I = (PageNo -1) * PageSize To PageNo * PageSize - 1
      If I > myTable.Rows.Count -1 Then Exit For
      Dim newRow As DataRow = tempTable.NewRow()
      For J = 0 To myTable.Columns.Count -1
        newRow(myTable.Columns(J).ColumnName) = myTable.Rows(I).Item(J)
      Next
      tempTable.Rows.Add(newRow)
    Next
    myRepeater.DataSource = tempTable
    myRepeater.DataBind()
  End Sub

 '此过程用来判断是否应该显示,首页等按钮。
sub showwhatbutton()
 if Showpagenumber.text = 1 then
   firstpage.visible = false
   prepage.visible = false
  else
   firstpage.visible = true
   prepage.visible = true 
  end if
 
  if Showpagenumber.text = showtotalpage.text  then
   lastpage.visible = false
   nextpage.visible = false
  else
   lastpage.visible = true
   nextpage.visible = true 
  end if
end sub
 
 
 '此程序用来创建快速换页菜单
  Sub PageListItem()
    Dim I As Integer
    PageList.Items.Clear()
    For I = 0 To ShowTotalPage.Text -1
      PageList.Items.Add("第 " & I + 1 & " 页")
    Next
  End Sub
 
  '页面第一次加载时执行此程序,调用bindlist和pagelistitem子程序并赋值
  Sub Page_Init(sender As Object, e As Eventargs)
    BindList(1)
    PageListItem()
 showwhatbutton()
  End Sub
 
 '响应下拉菜单 PageList 命令。
  Sub PageList_Change(sender As Object, e As EventArgs)
    BindList(PageList.SelectedIndex + 1)
 showwhatbutton()
  End Sub

'此过程会根据用户点取的按钮来决定应该显示哪个分页的数据
  sub pagechange(sender as object, e as commandeventargs)
    select case e.commandargument
   case "nextpage"
    BindList( Showpagenumber.text + 1)
   case "prepage"
    BindList( Showpagenumber.text - 1)
   case "firstpage"
    BindList( 1 )
   case "lastpage"
   BindList( showtotalpage.text - 1)
 
   '当用户在文本框里输入 数字的时候执行下面代码。
   '如果用户输入的数字大于总页数时,显示最后一页。如果小于首页时,显示首页。
   case "pagenumber"
     if pagenumber.text > showtotalpage.text  then
  pagenumber.text = showtotalpage.text
  bindlist(pagenumber.text)
  else if pagenumber.text < 1 then
  pagenumber.text = 1
     bindlist(pagenumber.text)
  else
  bindlist(pagenumber.text)
  end if
  end select
     showwhatbutton()
  end sub
</script>

<Html>
  <Body>
    <Form Runat="Server">
      收录专辑总数:<Asp:Label Runat="Server" Id="ShowTotalrecord" /><br>
   目前位于第<Asp:Label Runat="Server" Id="Showpagenumber" />页<br>
      <asp:linkbutton runat="server" ID="firstpage" OnCommand="pagechange" Text="首页" CommandArgument="firstpage"/>
   <asp:linkbutton runat="server" ID="lastpage" OnCommand="pagechange" Text="尾页" CommandArgument="lastpage"/>
   <asp:linkbutton runat="server" ID="prepage" OnCommand="pagechange" Text="上一页" CommandArgument="prepage"/>
   <asp:linkbutton runat="server" ID="nextpage" OnCommand="pagechange" Text="下一页" CommandArgument="nextpage"/>
    <br>
  
   总页数:<Asp:Label Runat="Server" Id="showtotalpage" /><br>
   转到:
   <asp:dropdownlist runat="Server" ID="PageList" AutoPostBack="True" OnSelectedIndexChanged="PageList_Change" />
  <asp:textbox runat="server" id="pagenumber" ontextchange="change"/>
   <asp:button runat="server" ID="numberbutton" OnCommand="pagechange" Text="GO" CommandArgument="pagenumber"/>
   <Asp:Repeater Runat="Server" Id="myRepeater">
        <ItemTemplate>          <Br>
          <Br>
          <table border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="147" rowspan="6"> <div align="center"><Img Src="<%# Container.DataItem("唱片图片") %>"></div></td>
              <td width="362">&nbsp;</td>
            </tr>
            <tr>
              <td>乐队:<%# Container.DataItem("乐队名") %></td>
            </tr>
            <tr>
              <td>唱片:<%# Container.DataItem("唱片名") %></td>
            </tr>
            <tr>
              <td>加入时间:<%# Container.DataItem("添加时间")%> </td>
            </tr>
            <tr>
              <td>推荐指数:<%# Container.DataItem("欣赏指数")%></td>
            </tr>
            <tr>
              <td height="41">唱片风格:<%# Container.DataItem("风格名")%></td>
            </tr>
          </table>
          <Br>
        </ItemTemplate>
      </Asp:Repeater>
    </Form>
  </Body>
</Html>

--------------------------------------------------------------------------------------------

practice_2

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script language="vb" runat="server">
 
 '定义bindlist子程序,调用pageno(页面位置)参数
  sub BindList(PageNo As Integer)
  
   '使用connection对象打开数据连接
    Dim ConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                               "Data Source=" & Server.MapPath("db/music.mdb")
   
 '定义sql
 Dim strSQL As String = "Select * From 唱片,风格 where 唱片.风格号码 = 风格.风格号码 Order By '添加时间' Desc"
  
  
    '创建dataadapter对象
 Dim objCmd As New OleDbDataAdapter(strSQL, ConnString)
   
 
 '创建dataset对象
 Dim DS As New DataSet()
   
 '调用dataadapter对象的fill()方法讲选取的数据置入dataset对象。
 objCmd.Fill(DS, "唱片")
  
    '声明mytable 变量为 datatable对象,并将ds.tables("歌曲")赋值给 mytable变量。
    Dim myTable As DataTable = DS.Tables("唱片")   
 dim pagesize as integer = 2
 
 '显示留言的总条数
 dim totalrecord as integer = mytable.rows.count
 showtotalrecord.text = totalrecord
 
 '显示流言总页数totalrecord为流言总条数
 dim totalpage as integer
 if totalrecord < pagesize then
   totalpage = 1
 else
   if totalrecord mod pagesize <> 0 then
     totalpage = totalrecord \ pagesize + 1
   else
     totalpage = totalrecord / pagesize
   end if
 end if
  ShowTotalPage.Text = TotalPage
   
    '创建 tempTable 数据表,其结构与mytable相同,tempTable 数据表
    '保存的数据会连接到 Repeater 控件,以显示留言内容。
 Dim tempTable As DataTable = myTable.Clone()
    Dim I, J As Integer
    For I = (PageNo -1) * PageSize To PageNo * PageSize - 1
      If I > myTable.Rows.Count -1 Then Exit For
      Dim newRow As DataRow = tempTable.NewRow()
      For J = 0 To myTable.Columns.Count -1
        newRow(myTable.Columns(J).ColumnName) = myTable.Rows(I).Item(J)
      Next
      tempTable.Rows.Add(newRow)
    Next
    myRepeater.DataSource = tempTable
    myRepeater.DataBind()
  End Sub
 

 '此过程用来判断是否应该显示,首页等按钮。
sub showwhatbutton()
 if pagelist.selectedindex= 0 then
   firstpage.visible = false
   prepage.visible = false
  else
   firstpage.visible = true
   prepage.visible = true 
  end if
 
  if pagelist.selectedindex = pagelist.items.count - 1 then
   lastpage.visible = false
   nextpage.visible = false
  else
   lastpage.visible = true
   nextpage.visible = true 
  end if
end sub 
 
 '此程序用来创建快速换页菜单
  Sub PageListItem()
    Dim I As Integer
    PageList.Items.Clear()
    For I = 0 To ShowTotalPage.Text -1
      PageList.Items.Add("第 " & I + 1 & " 页")
    Next
  End Sub
  '页面第一次加载时执行此程序,调用bindlist和pagelistitem子程序并赋值
  Sub Page_Init(sender As Object, e As Eventargs)
    BindList(1)
    PageListItem()
 showwhatbutton()
  End Sub
 
 '响应下拉菜单 PageList 命令。
  Sub PageList_Change(sender As Object, e As EventArgs)
    BindList(PageList.SelectedIndex + 1)
 showwhatbutton()
  End Sub
 
  sub pagechange(sender as object, e as commandeventargs)
    select case e.commandargument
   case "firstpage"
     pagelist.selectedindex = 0
   case "nextpage"
     pagelist.selectedindex  += 1
   case "prepage"
      pagelist.selectedindex  -= 1
   case "lastpage"
   pagelist.selectedindex = showtotalpage.text - 1
  end select
  bindlist(PageList.SelectedIndex + 1)
  showwhatbutton()
  end sub
</script>

<Html>
  <Body>
    <Form Runat="Server">
      收录专辑总数:<Asp:Label Runat="Server" Id="ShowTotalrecord" />
      <asp:linkbutton runat="server" ID="firstpage" OnCommand="pagechange" Text="首页" CommandArgument="firstpage"/>
    <asp:linkbutton runat="server" ID="lastpage" OnCommand="pagechange" Text="尾页" CommandArgument="lastpage"/>      
       <asp:linkbutton runat="server" ID="prepage" OnCommand="pagechange" Text="上一页" CommandArgument="prepage"/>      
       <asp:linkbutton runat="server" ID="nextpage" OnCommand="pagechange" Text="下一页" CommandArgument="nextpage"/>      
<br>
  
   总页数:<Asp:Label Runat="Server" Id="showtotalpage" />
   目前位于:<asp:dropdownlist runat="Server" ID="PageList" AutoPostBack="True" OnSelectedIndexChanged="PageList_Change" />
   <Asp:Repeater Runat="Server" Id="myRepeater">
        <ItemTemplate>          <Br>
          <Br>
          <table border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="147" rowspan="6"> <div align="center"><Img Src="<%# Container.DataItem("唱片图片") %>"></div></td>
              <td width="362">&nbsp;</td>
            </tr>
            <tr>
              <td>乐队:<%# Container.DataItem("乐队名") %></td>
            </tr>
            <tr>
              <td>唱片:<%# Container.DataItem("唱片名") %></td>
            </tr>
            <tr>
              <td>加入时间:<%# Container.DataItem("添加时间")%> </td>
            </tr>
            <tr>
              <td>推荐指数:<%# Container.DataItem("欣赏指数")%></td>
            </tr>
            <tr>
              <td height="41">唱片风格:<%# Container.DataItem("风格名")%></td>
            </tr>
          </table>
          <Br>
        </ItemTemplate>
      </Asp:Repeater>
    </Form>
  </Body>
</Html>

---------------------------------------------------------------

数据库可以按照代码里自行建立



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=373196

推广二维码
邮件订阅

如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

重磅专题