带 DataPager 的 ListView 不起作用

发布于 2024-07-08 01:32:01 字数 1116 浏览 13 评论 0原文

从我读过的所有内容来看,向 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

自此以后,行同陌路 2024-07-15 01:32:02

我们需要在 OnPreRender 事件中再次对列表视图进行数据绑定。

protected override void OnPreRender(EventArgs e)
        {
            ListView1.DataBind();
            base.OnPreRender(e);
        }

--更新

在使用asp.net ajax处理了一些列表视图之后,我看到了一个比上面的更有意义的解决方案。 您通常会在页面加载方法或按钮单击事件处理程序上对 Listview 进行数据绑定,并且当回发时,数据绑定将丢失,如上面的问题所述。 因此,我们需要在列表视图的页面属性更改事件处理程序上再次进行数据绑定。

ListView_PagePropertiesChanged(object sender, EventArgs e)
{
ListView.DataSource=someDatasource;
ListView.DataBind()
}

We need to databind list view again in OnPreRender event.

protected override void OnPreRender(EventArgs e)
        {
            ListView1.DataBind();
            base.OnPreRender(e);
        }

--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.

ListView_PagePropertiesChanged(object sender, EventArgs e)
{
ListView.DataSource=someDatasource;
ListView.DataBind()
}
拥有 2024-07-15 01:32:02

还有一个解决方案,很简单,只需从数据库中获取“QUERY-STRING”中的“ID”,然后将其设置为寻呼机控件属性,如 [ QueryStringField="ID" ],如下所示:

<asp:DataPager ID="DataPagerProducts" runat="server" QueryStringField="ID" PageSize="3">
                            <Fields>
                                <asp:NextPreviousPagerField ShowFirstPageButton="True" ShowNextPageButton="False" />
                                <asp:NumericPagerField />
                                <asp:NextPreviousPagerField ShowLastPageButton="True" ShowPreviousPageButton="False" />
                            </Fields>
                        </asp:DataPager>

注意:如果没有唤醒,则还要设置[ 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:

<asp:DataPager ID="DataPagerProducts" runat="server" QueryStringField="ID" PageSize="3">
                            <Fields>
                                <asp:NextPreviousPagerField ShowFirstPageButton="True" ShowNextPageButton="False" />
                                <asp:NumericPagerField />
                                <asp:NextPreviousPagerField ShowLastPageButton="True" ShowPreviousPageButton="False" />
                            </Fields>
                        </asp:DataPager>

Note: if not woking, then set also [ PagedControlID="ListView_Name" ].

久伴你 2024-07-15 01:32:02

看一下 ListViewPagedDataSource。

private ListViewPagedDataSource GetProductsAsPagedDataSource(DataView dv)
{
// Limit the results through a PagedDataSource
ListViewPagedDataSource pagedData = new ListViewPagedDataSource();
pagedData.DataSource = dv;
pagedData.MaximumRows = dv.Table.Rows.Count;
pagedData.TotalRowCount = dpTop.PageSize;

if (Request.QueryString[dpTop.QueryStringField] != null)
  pagedData.StartRowIndex = (Convert.ToInt32(Request.QueryString[dpTop.QueryStringField]) - 1) * dpTop.PageSize;
else
  pagedData.StartRowIndex = 0;

return pagedData;
}

不过,我在查看最后一页时遇到了问题。 DataPager跳回第一页,但显示的数据是最后一页。

Take a look at the ListViewPagedDataSource.

private ListViewPagedDataSource GetProductsAsPagedDataSource(DataView dv)
{
// Limit the results through a PagedDataSource
ListViewPagedDataSource pagedData = new ListViewPagedDataSource();
pagedData.DataSource = dv;
pagedData.MaximumRows = dv.Table.Rows.Count;
pagedData.TotalRowCount = dpTop.PageSize;

if (Request.QueryString[dpTop.QueryStringField] != null)
  pagedData.StartRowIndex = (Convert.ToInt32(Request.QueryString[dpTop.QueryStringField]) - 1) * dpTop.PageSize;
else
  pagedData.StartRowIndex = 0;

return pagedData;
}

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.

坦然微笑 2024-07-15 01:32:02

另外,如果 ListView 的数据源发生更改(例如,如果基于搜索参数显示数据),请不要忘记在每次更新数据源时重置分页器。 对于 ListView,这并不像其他一些数据绑定控件(例如 GridView)那么简单:

private void ResetListViewPager()
{
    DataPager pager = (DataPager)ListViewMembers.FindControl("DataPager1");
    if (pager != null)
    {
        CommandEventArgs commandEventArgs = new CommandEventArgs(DataControlCommands.FirstPageCommandArgument, "");
        // MAKE SURE THE INDEX IN THE NEXT LINE CORRESPONDS TO THE CORRECT FIELD IN YOUR PAGER
        NextPreviousPagerField nextPreviousPagerField = pager.Fields[0] as NextPreviousPagerField;
        if (nextPreviousPagerField != null)
        {
            nextPreviousPagerField.HandleEvent(commandEventArgs);
        }

        // THIS COMMENTED-OUT SECTION IS HOW IT WOULD BE DONE IF USING A NUMERIC PAGER RATHER THAN A NEXT/PREVIOUS PAGER
        //commandEventArgs = new CommandEventArgs("0", "");
        //NumericPagerField numericPagerField = pager.Fields[0] as NumericPagerField;
        //if (numericPagerField != null)
        //{
        //    numericPagerField.HandleEvent(commandEventArgs);
        //}
    }
}

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):

private void ResetListViewPager()
{
    DataPager pager = (DataPager)ListViewMembers.FindControl("DataPager1");
    if (pager != null)
    {
        CommandEventArgs commandEventArgs = new CommandEventArgs(DataControlCommands.FirstPageCommandArgument, "");
        // MAKE SURE THE INDEX IN THE NEXT LINE CORRESPONDS TO THE CORRECT FIELD IN YOUR PAGER
        NextPreviousPagerField nextPreviousPagerField = pager.Fields[0] as NextPreviousPagerField;
        if (nextPreviousPagerField != null)
        {
            nextPreviousPagerField.HandleEvent(commandEventArgs);
        }

        // THIS COMMENTED-OUT SECTION IS HOW IT WOULD BE DONE IF USING A NUMERIC PAGER RATHER THAN A NEXT/PREVIOUS PAGER
        //commandEventArgs = new CommandEventArgs("0", "");
        //NumericPagerField numericPagerField = pager.Fields[0] as NumericPagerField;
        //if (numericPagerField != null)
        //{
        //    numericPagerField.HandleEvent(commandEventArgs);
        //}
    }
}
许一世地老天荒 2024-07-15 01:32:02

在数据分页器的预渲染事件中绑定列表视图,而不是在页面加载时绑定。 请参阅此处的解决方案

Bind the listview at datapager's pre render event not at page load. Please see the solution here

猫弦 2024-07-15 01:32:02
<asp:ListView ID="ListView1" runat="server" DataSourceID="sdsImages">
    <ItemTemplate>
        <div class="photo sample12">
                <asp:Image ID="img_Galerie" runat="server" ImageUrl='<%# "~/imageHandler.ashx?ID=" + Eval("ImageID") %>' />
        </div>
    </ItemTemplate>
</asp:ListView>
<asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" PageSize="3" QueryStringField="ImageID">
    <Fields>
        <asp:NextPreviousPagerField ShowFirstPageButton="True" ShowNextPageButton="False" />
        <asp:NumericPagerField />
        <asp:NextPreviousPagerField ShowLastPageButton="True" ShowPreviousPageButton="False" />
    </Fields>
</asp:DataPager>
<asp:SqlDataSource ID="sdsImages" runat="server"
    ConnectionString="<%$ ConnectionStrings:DBCS %>"
    SelectCommand="SELECT ImageID FROM  Images ">

<asp:ListView ID="ListView1" runat="server" DataSourceID="sdsImages">
    <ItemTemplate>
        <div class="photo sample12">
                <asp:Image ID="img_Galerie" runat="server" ImageUrl='<%# "~/imageHandler.ashx?ID=" + Eval("ImageID") %>' />
        </div>
    </ItemTemplate>
</asp:ListView>
<asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" PageSize="3" QueryStringField="ImageID">
    <Fields>
        <asp:NextPreviousPagerField ShowFirstPageButton="True" ShowNextPageButton="False" />
        <asp:NumericPagerField />
        <asp:NextPreviousPagerField ShowLastPageButton="True" ShowPreviousPageButton="False" />
    </Fields>
</asp:DataPager>
<asp:SqlDataSource ID="sdsImages" runat="server"
    ConnectionString="<%$ ConnectionStrings:DBCS %>"
    SelectCommand="SELECT ImageID FROM  Images ">

乱了心跳 2024-07-15 01:32:02

尝试这个:

protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
    DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
    ListView1.DataSource = productList;
    ListView1.DataBind();
    DataPager1.DataBind();
}

try this:

protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
    DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
    ListView1.DataSource = productList;
    ListView1.DataBind();
    DataPager1.DataBind();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文