分页显示数据

本节以SQL Server为例,介绍一下如何分页显示后台数据,原理和我们在快速入门这一章介绍的相同,在合成的链接地址中包括要现实的页码:

http://127.0.0.1/list.htm?page=1

下面是HttpRequest事件代码:

Select Case e.Path
    Case "list.htm"
       
'
获取要显示的页
        Dim page As Integer = 0
'
默认page0,显示第一页
        Dim pageRows As Integer = 15
'
每页15
        If e.GetValues.ContainsKey("page") Then 
'
如果地址中有page参数
            Integer.TryParse(e.GetValues("page"), page)
'
提取page参数
        End If
        Dim StartRow As Integer = page * pageRows + 1
'
此页第一行
        Dim EndRow As Integer = (page + 1) * pageRows
'
此页最后一行       

        '获取该页数据
        Dim cmd As New SQLCommand
        cmd.ConnectionName = "orders"
'
记得设置数据源名称
        cmd.CommandText =
"Select Count(*) From {
订单}"
        Dim Count As Integer = cmd.ExecuteScalar()
'
获取总的行数
        cmd.CommandText =
"Select * From (Select Row_Number() Over(Order by
日期) As [NO.], 产品,客户,数量,单价,日期 From 订单) As a "
        cmd.CommandText = cmd.CommandText & "  Where [NO.]>= " & StartRow & " And [NO.] <= " & EndRow
        Dim dt As DataTable = cmd.ExecuteReader       

        '根据此页数据生成网页
       
Dim wb As New WeUI
        With wb.AddTable("","Table1")
            .CreateFromDataTable(dt)
        End With
        With wb.AddButtonGroup("","btg2", False)
            If page > 0 Then
                .Add("btnPrev",
"
上一页","","List.htm?page=" & page - 1)
            End If
            If Endrow < count Then
                .Add("btnNext",
"
下一页","","List.htm?page=" & page + 1)
            End If
        End With
        e.WriteString(wb.Build)

End
Select

这是通过手机访问显示的效果:


本页地址:http://www.foxtable.com/mobilehelp/topics/0114.htm