带 DataPager 的 ListView 不起作用
从我读过的所有内容来看,向 ListView 控件添加分页似乎应该非常简单,但它对我不起作用。 将 ListView 和 DataPager 控件添加到窗体并将它们连接在一起后,我得到了非常奇怪的行为。 DataPager 正确限制了 ListView 的页面大小,但单击分页按钮根本不会影响 ListView。 分页按钮似乎认为它们正在做它们的工作,因为当您转到最后一页时,最后一个按钮被禁用,等等,但 ListView 永远不会改变。 此外,需要在 DataPager 上单击两次才能使其执行任何操作,即,单击“最后”一次不会执行任何操作,但第二次单击会导致 DataPager 做出反应,就好像现在选择了最后一页一样。
我唯一能想到的是我在运行时绑定 DataSource(到 LINQ 对象),而不是使用 LinqDataSource 控件或任何东西。 有人见过这种行为吗? 难道我做错了什么? 这是我正在使用的代码:
<asp:DataPager ID="HistoryDataPager" runat="server" PagedControlID="HistoryListView" PageSize="10">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="true" ShowLastPageButton="true" />
</Fields>
</asp:DataPager>
<asp:ListView ID="HistoryListView" runat="server">
...
</asp:ListView>
在代码隐藏中:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
HistoryListView.DataSource = From x in myContext.myTables ...
DataBind()
End If
End Sub
From everything I've read, it seemed that adding paging to a ListView control should be dead simple, but it's not working for me. After adding the ListView and DataPager controls to the form and wiring them together, I'm getting very odd behavior. The DataPager correctly limits the ListView's page size, but clicking the paging buttons doesn't affect the ListView at all. The paging buttons seem to think they are doing they're job, as the last button is disabled when you go to the last page, etc., but the ListView never changes. Also, it takes two clicks on the DataPager to get it to do anything, i.e., clicking on Last once does nothing, but clicking it a second time causes the DataPager to react as if the last page is now selected.
The only thing I can think of is that I'm binding the DataSource at runtime (to a LINQ object), not using a LinqDataSource control or anything. Has anyone seen this behavior? Am I doing something wrong? Here's the code I'm using:
<asp:DataPager ID="HistoryDataPager" runat="server" PagedControlID="HistoryListView" PageSize="10">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="true" ShowLastPageButton="true" />
</Fields>
</asp:DataPager>
<asp:ListView ID="HistoryListView" runat="server">
...
</asp:ListView>
In the code-behind:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
HistoryListView.DataSource = From x in myContext.myTables ...
DataBind()
End If
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我们需要在 OnPreRender 事件中再次对列表视图进行数据绑定。
--更新
在使用asp.net ajax处理了一些列表视图之后,我看到了一个比上面的更有意义的解决方案。 您通常会在页面加载方法或按钮单击事件处理程序上对 Listview 进行数据绑定,并且当回发时,数据绑定将丢失,如上面的问题所述。 因此,我们需要在列表视图的页面属性更改事件处理程序上再次进行数据绑定。
We need to databind list view again in OnPreRender event.
--Update
After working on a few list views with asp.net ajax, I saw a solution that makes more sense than the above one. You would normally data bind Listview on page load method or a button click event handler and when there is post back the data binding would be lost as described above in the problem. So, we need to data bind again on page properties changed event handler for the list view.
还有一个解决方案,很简单,只需从数据库中获取“QUERY-STRING”中的“ID”,然后将其设置为寻呼机控件属性,如 [ QueryStringField="ID" ],如下所示:
注意:如果没有唤醒,则还要设置
[ PagedControlID="ListView_Name" ]
。One More Solution, Its Simple, Just Get "ID" in "QUERY-STRING" from the Database, Now Set it to the Pager Control Property as [ QueryStringField="ID" ] like:
Note: if not woking, then set also
[ PagedControlID="ListView_Name" ]
.看一下 ListViewPagedDataSource。
不过,我在查看最后一页时遇到了问题。 DataPager跳回第一页,但显示的数据是最后一页。
Take a look at the ListViewPagedDataSource.
Though, I have a problem viewing the last page. The DataPager jumps back to the first page, but the data displayed is the last page.
另外,如果 ListView 的数据源发生更改(例如,如果基于搜索参数显示数据),请不要忘记在每次更新数据源时重置分页器。 对于 ListView,这并不像其他一些数据绑定控件(例如 GridView)那么简单:
Also, if the data source of your ListView is changed (e.g. if displaying data based on search parameters), don't forget to reset the pager every time the data source is updated. With a ListView this is not as straightforward as some other data-bound controls (e.g. GridView):
在数据分页器的预渲染事件中绑定列表视图,而不是在页面加载时绑定。 请参阅此处的解决方案
Bind the listview at datapager's pre render event not at page load. Please see the solution here
尝试这个:
try this: