自己实现的一个分页-用户控件
前台代码
Public Class page
Inherits System.Web.UI.UserControl
![](/Images/OutliningIndicators/InBlock.gif)
Web 窗体设计器生成的代码
Public usersGrid As DataGrid
![](/Images/OutliningIndicators/InBlock.gif)
Public Event MyEvent()
![](/Images/OutliningIndicators/InBlock.gif)
Private _str As String
Public Property str() As String
Get
Return _str
End Get
Set(ByVal Value As String)
_str = Value
End Set
End Property
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
If Not IsPostBack Then
End If
End Sub
Public Sub initMe()
initDrop()
initSelf()
End Sub
Private Sub initDrop()
'构建选页下拉列表lstPageSelector,并选中显示其当前页值
Dim i As Integer
DrpPageList.Items.Clear()
For i = 1 To usersGrid.PageCount
DrpPageList.Items.Add(i)
If i = (usersGrid.CurrentPageIndex + 1) Then
Me.DrpPageList.Items.Item(i - 1).Selected = True
End If
Next
End Sub
![](/Images/OutliningIndicators/InBlock.gif)
Private Sub iniDropState()
Me.DrpPageList.SelectedIndex = -1
Me.DrpPageList.Items.FindByText((usersGrid.CurrentPageIndex + 1).ToString).Selected = True
End Sub
Private Sub initSelf()
On Error Resume Next
'根据当前页值判断linkbutton上页/下页的enabled与否
If usersGrid.PageCount = 1 Then
Hfirst.Enabled = False
Hpre.Enabled = False
Hnext.Enabled = False
Hend.Enabled = False
ElseIf usersGrid.PageCount = 0 Then
Hfirst.Enabled = False
Hpre.Enabled = False
Hnext.Enabled = False
Hend.Enabled = False
Else
Select Case usersGrid.CurrentPageIndex
Case 0
Hfirst.Enabled = False
Hpre.Enabled = False
Hnext.Enabled = True
Hend.Enabled = True
Case (usersGrid.PageCount - 1)
Hfirst.Enabled = True
Hpre.Enabled = True
Hnext.Enabled = False
Hend.Enabled = False
Case Else
Hfirst.Enabled = True
Hpre.Enabled = True
Hnext.Enabled = True
Hend.Enabled = True
End Select
End If
Me.Label1.Text = "当前 [" + (usersGrid.CurrentPageIndex + 1).ToString + "/" + usersGrid.PageCount.ToString + "] 页 每页 [" + usersGrid.PageSize.ToString + "] 条记录 "
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
End Sub
![](/Images/OutliningIndicators/InBlock.gif)
Private Sub Hend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Hend.Click
' Cancel edit mode
usersGrid.EditItemIndex = -1
usersGrid.CurrentPageIndex = usersGrid.PageCount - 1
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
Call MyEvents()
End Sub
![](/Images/OutliningIndicators/InBlock.gif)
Public Sub InitPagerControls(ByRef grid As DataGrid)
Me.usersGrid = grid
![](/Images/OutliningIndicators/InBlock.gif)
End Sub
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
Private Sub Hfirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Hfirst.Click
usersGrid.CurrentPageIndex = 0
usersGrid.EditItemIndex = -1
Call MyEvents()
End Sub
![](/Images/OutliningIndicators/InBlock.gif)
Private Sub Hpre_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Hpre.Click
' Cancel edit mode
usersGrid.EditItemIndex = -1
If (usersGrid.CurrentPageIndex > 0) Then usersGrid.CurrentPageIndex = usersGrid.CurrentPageIndex - 1
Call MyEvents()
End Sub
![](/Images/OutliningIndicators/InBlock.gif)
Private Sub Hnext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Hnext.Click
' Cancel edit mode
usersGrid.EditItemIndex = -1
If (usersGrid.CurrentPageIndex < (usersGrid.PageCount - 1)) Then usersGrid.CurrentPageIndex = usersGrid.CurrentPageIndex + 1
Call MyEvents()
End Sub
![](/Images/OutliningIndicators/InBlock.gif)
Private Sub DrpPageList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DrpPageList.SelectedIndexChanged
On Error Resume Next
usersGrid.CurrentPageIndex = CInt(Me.DrpPageList.SelectedValue) - 1
' Cancel edit mode
usersGrid.EditItemIndex = -1
Call MyEvents()
End Sub
![](/Images/OutliningIndicators/InBlock.gif)
Private Sub MyEvents()
RaiseEvent MyEvent()
initSelf()
iniDropState()
End Sub
End Class
![](/Images/OutliningIndicators/None.gif)
调用例子
Public Page1 As Page
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
policyAdmin(Page, "admin")
Page1.InitPagerControls(Me.DataGrid1)
AddHandler Page1.MyEvent, AddressOf bind
If Not IsPostBack Then
bind()
End If
![](/Images/OutliningIndicators/InBlock.gif)
End Sub
1
<%@ Control Language="vb" AutoEventWireup="false" Codebehind="page.ascx.vb" Inherits="course.page" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
2
3
<div align="right">
4
<table width="100%">
5
<TBODY>
6
<tr>
7
<td>
8
9
10
11
<asp:label id="Label1" runat="server">当前 x/n 页 共3条记录</asp:label>
12
13
<asp:linkbutton id="Hfirst" runat="server">[首页]</asp:linkbutton>
14
<asp:linkbutton id="Hpre" runat="server">[上页]</asp:linkbutton>
15
<asp:linkbutton id="Hnext" runat="server">[下页]</asp:linkbutton>
16
<asp:linkbutton id="Hend" runat="server">[末页]</asp:linkbutton>
17
<asp:dropdownlist id="DrpPageList" runat="server" AutoPostBack="True"></asp:dropdownlist>
18
19
</td></tr></TBODY></table></div>
20
后台代码![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/None.gif)
10
![](/Images/OutliningIndicators/None.gif)
11
![](/Images/OutliningIndicators/None.gif)
12
![](/Images/OutliningIndicators/None.gif)
13
![](/Images/OutliningIndicators/None.gif)
14
![](/Images/OutliningIndicators/None.gif)
15
![](/Images/OutliningIndicators/None.gif)
16
![](/Images/OutliningIndicators/None.gif)
17
![](/Images/OutliningIndicators/None.gif)
18
![](/Images/OutliningIndicators/None.gif)
19
![](/Images/OutliningIndicators/None.gif)
20
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
调用例子
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)