首页 > 开发 > .Net > 正文

用ASP.NET实现网络空间管理

2020-02-03 16:37:51
字体:
来源:转载
供稿:网友
    一、程序功能:为repeater实现分页 
   
    二、窗体设计: 
   
    1、新建asp.net web应用程序,命名为repeater2,保存路径为http://192.168.0.1/repeater2(注:我机子上的网站的ip是192.168.0.1的主目录是d:/web文件夹)然后点击确定。 
   
    2、向窗体添加一个3行一列的表,向表的第一行中添加一个repeater控件,向表的第二行中添加两个label控件向表的第三行中添加四个button按钮。 
   
    3、切换到html代码窗口,在<asp:repeater id="repeater1" runat="server">和</asp:repeater>之间添加以下代码:
  
   <itemtemplate>
   <table id="table2" width="498">
    <tr>
     <td><%#databinder.eval(container,"dataitem.employeeid")%></td>
     <td><%#databinder.eval(container,"dataitem.lastname")%></td>
    </tr>
   </table>
   </itemtemplate> 
   
     三、代码设计:
  
   imports system.data.sqlclient
   public class webform1
   inherits system.web.ui.page
  
    dim scon as new sqlconnection("server=localhost;database=northwind;uid=sa;pwd=123")
    dim sda as sqldataadapter
    dim ds as dataset
    dim currentpage as integer '记录着目前在哪一页上
    dim maxpage as integer '总共有多少页
    const rowcount as integer = 3 '一页有多少行
    dim rowsum as integer '总共有多少行
  
    '窗体代码省略
  
    private sub page_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
  
    if not page.ispostback then
     sda = new sqldataadapter("select employeeid, lastname from employees order by employeeid", scon)
     ds = new dataset
     try
      sda.fill(ds, "employees")
      '获取总共有多少行
      rowsum = ds.tables(0).rows.count
     catch ex as exception
      rowsum = 0
     end try
  
     '如果没有数据,退出过程
     if rowsum = 0 then exit sub
     '计算出浏览数据的总页数
     if rowsum mod rowcount > 0 then
      '有余数要加1
      maxpage = rowsum / rowcount + 1
     else
      '正好除尽
      maxpage = rowsum / rowcount
     end if
  
     currentpage = 1
     '调用绑定数据过程
     readpage(currentpage)
     binddata()
     label2.text = maxpage
     '首页和上一页按钮不可见
     button1.visible = false
     button2.visible = false
    end if
   end sub
  
   '创建一个绑定数据的过程
   sub binddata()
    repeater1.datasource = ds
    repeater1.databind()
    label1.text = currentpage
   end sub
  
   '创建一个填充数据集的过程
   sub readpage(byval n as integer)
    sda = new sqldataadapter("select employeeid, lastname from employees order by employeeid", scon)
    ds = new dataset
    ds.clear()
    sda.fill(ds, (n - 1) * rowcount, rowcount, "employees")
   end sub
  
   '首页按钮
   private sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click
  
    currentpage = 1
    '调用填充数据集过程
    readpage(currentpage)
    '绑定数据
    binddata()
    '设置首页、第一页按钮不可见,显示下一页尾页按钮
    button1.visible = false
    button2.visible = false
    button3.visible = true
    button4.visible = true
  
   end sub
  
   '上一页按钮
   private sub button2_click(byval sender as system.object, byval e as system.eventargs) handles button2.click
   '如果现在页是第二页,设置首页和上一页按钮不可见
    if label1.text > 2 then
     button3.visible = true
     button4.visible = true
    else
     button1.visible = false
     button2.visible = false
     button3.visible = true
     button4.visible = true
    end if
    currentpage = label1.text - 1
    readpage(currentpage)
    binddata()
   end sub
  
   '下一页按钮
   private sub button3_click(byval sender as system.object, byval e as system.eventargs) handles button3.click
   '如果现在页倒数第二页,设置最后页和下一页按钮不可见
    if label1.text < label2.text - 1 then
     button1.visible = true
     button2.visible = true
    else
     button1.visible = true
     button2.visible = true
     button3.visible = false
     button4.visible = false
    end if
     currentpage = label1.text + 1
     readpage(currentpage)
     binddata()
    end sub
  
    '尾页按钮
    private sub button4_click(byval sender as system.object, byval e as system.eventargs) handles button4.click
     '设置当前页为最大页数
     currentpage = label2.text
     readpage(currentpage)
     binddata()
     button1.visible = true
     button2.visible = true
     button3.visible = false
     button4.visible = false
    end sub
   end class
  
     窗体界面如下所示
  
  

商业源码热门下载www.html.org.cn

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表