//前台
<p align="center" >
页码数:<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:LinkButton ID="LinkButton1" runat="server" >首页</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" >下一页</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" >上一页</asp:LinkButton>
<asp:LinkButton ID="LinkButton4" runat="server" >最后一页</asp:LinkButton>
<asp:TextBox ID="TextBox1" runat="server" Width="80px" ></asp:TextBox>
<asp:LinkButton ID="LinkButton5" runat="server" >跳</asp:LinkButton>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="请输入数字" ValidationExpression="^[0-9]*$"></asp:RegularExpressionValidator></p>
//后台(VB)
Partial Class 后台管理_Xuser
Inherits System.Web.UI.Page
Dim pagesize As Integer = 0
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
pagesize = 5
'显示全部信息
If Not Page.IsPostBack Then
'获取总记录数
Dim sqlstr As String = "select isnull(count(name),0) from Xuser "
Count = sqlhelper.Executescalar(sqlhelper.connstr, CommandType.Text, sqlstr)
getspeak()
End If
End Sub
'分页函数
Public Sub getspeak()
Dim type2 As String = "SELECT TOP " & pagesize & " * FROM xuser WHERE Xid not in ( SELECT TOP " & pagesize * (PageIndex - 1) & " xid FROM xuser ORDER BY xid ) ORDER BY xid "
Dim type As String = "select *from(select top " & pagesize & " * from (select top " & pagesize * PageIndex & "* from xuser order by xid asc) as aSysTable order by xid desc)as bSysTable order by xid asc"
Dim par(1) As SqlParameter
par(0) = New SqlParameter("pagesize", SqlDbType.NVarChar, 50)
par(0).Value = pagesize
par(1) = New SqlParameter("PageIndex", SqlDbType.NVarChar, 50)
par(1).Value = PageIndex
Label1.Text = String.Format("当前是第{0}页/共{1}页", PageIndex, PageCount)
Dim ds As DataSet = sqlhelper.ExecuteDataSet(sqlhelper.connstr, CommandType.Text, type, par)
GridView1.DataSource = ds.Tables(0)
GridView1.DataBind()
End Sub
'页 索引
Public Property PageIndex() As Integer
Get
If ViewState("pageIndex") Is Nothing Then
Return 1
Else
Return CType(ViewState("pageIndex"), Integer)
End If
End Get
Set(ByVal Value As Integer)
ViewState("pageIndex") = value
End Set
End Property
'/ <summary>
'/ 总记录数
'/ </summary>
Public Property Count() As Integer
Get
If ViewState("count") Is Nothing Then
Return 0
Else
Return CType(ViewState("count"), Integer)
End If
End Get
Set(ByVal Value As Integer)
ViewState("count") = value
End Set
End Property
'/ <summary>
'/ 总页数
'/ </summary>
Public ReadOnly Property PageCount() As Integer
Get
If Count Mod pagesize = 0 Then
Return Count / pagesize
Else
Return (Count \ pagesize) + 1
End If
End Get
End Property
'首页
Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles LinkButton1.Click
PageIndex = 1
getspeak()
End Sub
'下一页
Protected Sub LinkButton2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles LinkButton2.Click
If PageIndex < PageCount Then
PageIndex = PageIndex + 1
End If
getspeak()
End Sub
'上一页
Protected Sub LinkButton3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles LinkButton3.Click
If PageIndex > 1 Then
PageIndex = PageIndex - 1
End If
getspeak()
End Sub
'最后一页
Protected Sub LinkButton4_Click(ByVal sender As Object, ByVal e As EventArgs) Handles LinkButton4.Click
PageIndex = PageCount
getspeak()
End Sub
'跳
Protected Sub LinkButton5_Click(ByVal sender As Object, ByVal e As EventArgs) Handles LinkButton5.Click
If Not TextBox1.Text = "" Then
Dim index As Integer = Int32.Parse(TextBox1.Text)
If index > 0 And index <= PageCount Then
PageIndex = index
getspeak()
End If
Else
MsgBox("必须输入数字!")
End If
End Sub