在搜索结果中实现可点击的页码
经典场景:接受用户输入,获取搜索结果并将其在页面中显示给用户。 然后,我需要显示“第一个”、“下一个”、“上一个”等按钮,并将用户当前页面维护在视图状态中。 一切都很好,工作正常。
然后我需要实现可点击的页码,即。 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
Classic scenario: Take user input, get a search-result and display it in pages to the user. I then need to display buttons for First, Next, Previous etc, and I maintain the users current page in viewstate. All is good, works fine.
Then I need to implement clickable page numbers, ie. 1-2-3-4-5-6 etc.
Rendering them is simple. I generate a linkbutton control at runtime, add commandargument with the page number and add a handler to it, so click are to be handled. Then I add it to a placeholder, and it is displayed as expected.
But then... If I did not already have a shaved head, I would be pulling out my hair getting the events to fire as expected every time.
How should I do this, so my events are always wired up and able to fire when the paging-linkbuttons are called?
Below is the important parts of the code, some pseudo to make it (hopefully) easier to understand, what I am doing.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我将建议不要使用 LinkButton,并推荐使用 Hyperlinks / QueryString 参数。 出于以下几个原因:
您可以将 CurrentPage 方法重新定义为(希望这是正确的,我在 C# 方面比 vb.net 更好):
然后只需为每个页面添加超链接。
替代方案:如果您想使用 LinkButton,您可能需要考虑将单个 LinkButton 放入转发器中。 那么您唯一需要担心的事件是 OnItemCommand 事件。 然后就没有动态控件或事件。 像这样的事情:
将此控件绑定到连续整数的数组(或列表)(有多少页)。 然后在 doPaging 函数(我这样称呼它)中,检查 RepeaterCommandEventArgs.CommandArgument 以获取页码。
I'm going to recommend against a LinkButton and recommend Hyperlinks / QueryString parameters instead. For several reasons:
You would redefine your CurrentPage method as (hopefully this is correct, I'm better at C# than vb.net):
Then just add hyperlinks for each page.
Alternative: If you want to use the LinkButton, you might want to consider putting a single LinkButton in a repeater. Then the only event you have to worry about is the OnItemCommand event. Then no dynamic controls or events. Something like this:
Bind this control to an array (or list) of consecutive Integers (as many are there are pages). Then in your doPaging function (as I call it), check RepeaterCommandEventArgs.CommandArgument to get the page number.
谢谢你们的回答,伙计们。 我首先尝试了 Austins,但我一定错过了一些东西,因为我一直得到链接按钮的相同行为,仅每秒工作一次......所以我放弃了,并看到了 Keltex 中继器的替代解决方案! 它既出色又简单,而且我们不必担心任何页面生命周期的废话。
它确实有效! ;)
如果有人将来需要类似的东西,这里是幕后的相关代码:
Thanks for the answers, guys. I tried out Austins first, but I must be missing something, because I keep getting the same behavior of link buttons only working every second time... So I gave up on that, and saw the alternative solution with the repeater by Keltex! It is as brilliant as it is simple, and we don't have to worry about any page life-cycle bullshit.
It just really works! ;)
If somebody should need something similar in the future, here is the relevant code behind the scenes:
此代码有效(抱歉,它是用 C# 编写的):
This code works (sorry it's in C#):
您可以使用 DataPager 控件 - 唯一的限制是您必须将其与 ListView 控件一起使用,但您应该能够相当轻松地使用 ListView 控件来表示数据,因为它非常灵活。 您可以将 ListView 控件的 DataSource 设置为数据结果的结果,无论是 DataSet、Collection、Array 等。
要创建具有“first”、“last”和页码的分页控件,请设置像这样的 DataPager(其中 ListView1 是 ListView 控件的 ID):
根据设计,DataPager 使用数据库中的整个结果集,但您可以通过缓存结果并在后续请求中使用该结果来提高性能。
希望这可以帮助。
You could use the DataPager control -- the only limitation is you have to use it with the ListView control, but you should be able to represent your data using the ListView control fairly easily because it is very flexible. You can set the DataSource of the ListView control to the result of your data result, whether that be a DataSet, Collection, Array, etc.
To create the paging controls with "first", "last", and page numbers, set up the DataPager like this (where ListView1 is the ID of your ListView control):
By design, the DataPager uses the whole result set from the database, but you can improve the performance by caching the result and using that on the subsequent requests.
Hope this helps.
iirc...在运行时动态添加控件有点棘手。 控制树必须在回发期间重建...但在加载视图状态之前(不确定在页面生命周期中的时间...但在页面加载之前)。 所以...你的问题是,当 ASP.NET 试图找出你的事件时,实际的原始控件尚未创建。
iirc... adding controls dynamically at runtime is a bit tricky. The control tree must be rebuilt during post back... but before viewstate is loaded (not sure when in the page life cycle... but way before page load). So... your problem is that by the time asp.net is trying to figure out your event the actual originating control has not yet been created.