经典场景:获取用户输入,获取搜索结果并在页面中显示给用户。然后,我需要显示 First、Next、Previous 等按钮,并将用户当前页面保持在 viewstate 中。一切都很好,工作正常。
然后我需要实现可点击的页码,即。1-2-3-4-5-6 等
渲染它们很简单。我在运行时生成一个链接按钮控件,添加带有页码的命令参数并向其添加处理程序,因此要处理单击。然后我将它添加到占位符中,并按预期显示。
但是...
我应该怎么做,所以我的事件总是连接起来并且能够在调用分页链接按钮时触发?
下面是代码的重要部分,一些伪代码使其(希望)更容易理解,我在做什么。
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Search()
End If
End Sub
Sub Search
'Misc databinding stuff, searches and displays results for the page specified in Me.CurrentPage
RenderPagingControls()
End Sub
Sub RenderPagingControls
'loop throug pagenumbers, Build a linkbutton control, add it to a placeholder
AddHandler lbn.Click, AddressOf lbnNumber_Click
lblPageNumbers.Controls.Add(lbn)
...
End Sub
Protected Sub lbnNumber_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim b As LinkButton = CType(sender, LinkButton)
Me.CurrentPage = CInt(b.CommandArgument)
Search()
End Sub
Public Property CurrentPage() As Integer
Get
Dim o As Object = Me.ViewState("CurrentPage")
If o Is Nothing Then
Return 1
Else
Return CType(o, Integer)
End If
End Get
Set(ByVal value As Integer)
Me.ViewState("CurrentPage") = value
End Set
End Property
Protected Sub lbnNumber_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim b As LinkButton = CType(sender, LinkButton)
Me.CurrentPage = CInt(b.CommandArgument)
Search()
End Sub