ASP.NET DataGrid 和自定义分页

发布于 2024-07-11 18:46:59 字数 1668 浏览 7 评论 0原文

我正在尝试在 ASP.NET 中实现 DataGrid,并希望实现自定义分页,这样我就不必一次性提供所有数据。 我花了几个小时在互联网上研究,但没有发现任何有用的东西。

当我查看该页面时,我在网格中看到第一组结果,并且上一个链接被禁用。 然而,当我单击“下一步”时,我再次看到网格的第一页,并且上一个链接被禁用。 调试代码时,我确定从未调用 MyGrid_PageIndexChanged() 事件处理程序。

我在下面包含了我的简化代码。 我更改了变量名称并省略了方法以专注于数据网格分页问题。

在 ASPX 文件中:

<asp:DataGrid ID="myGrid" runat="server" GridLines="None" UseAccessibleHeader="true" AutoGenerateColumns="false" AllowPaging="true" AllowCustomPaging="true" PageIndexChanged="MyGrid_PageIndexChanged">
<PagerStyle Mode="NextPrev" NextPageText="Next >" PrevPageText="< Previous" />

<Columns>
<asp:BoundColumn HeaderText="Title" DataField="Name" />
<asp:BoundColumn HeaderText="Date" DataField="Date" />
</Columns>
</asp:DataGrid>

在 CS 文件中:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
                            myGrid.PageSize = 20;
                            myGrid.VirtualItemCount = GetNumItems();
            BindMyGrid();
        }
    }

    protected void MyGrid_PageIndexChanged(object sender, DataGridPageChangedEventArgs e)
    {
        myGrid.CurrentPageIndex = e.NewPageIndex;
        BindMyGrid();
    }

    private int GetNumItems()
    {
        return 500;
    }

    private void BindMyGrid()
    {
            Data[] array = GetDataFromInternetSomehow();
            this.myGrid.DataSource = array;
            this.myGrid.DataBind();
    }

    private class Data
    {
        public string Date { get; set; }
        public string Name { get; set; }
    }

对此的任何想法将不胜感激。

I'm trying to implement a DataGrid in ASP.NET, and want to achieve custom paging so that I don't have to provide all the data in one go. I've spent several hours researching on the internet, but haven't found anything useful.

When I view the page I see the first set of results in the grid, with the previous link disabled. When I click next however, I once again see the first page of the grid with the previous link disabled. When debugging the code I ascertained that the MyGrid_PageIndexChanged() event handler is never called.

I've included my simplified code below. I've changed variable names and omited methods to focus on the datagrid paging issue.

In the ASPX file:

<asp:DataGrid ID="myGrid" runat="server" GridLines="None" UseAccessibleHeader="true" AutoGenerateColumns="false" AllowPaging="true" AllowCustomPaging="true" PageIndexChanged="MyGrid_PageIndexChanged">
<PagerStyle Mode="NextPrev" NextPageText="Next >" PrevPageText="< Previous" />

<Columns>
<asp:BoundColumn HeaderText="Title" DataField="Name" />
<asp:BoundColumn HeaderText="Date" DataField="Date" />
</Columns>
</asp:DataGrid>

And in the CS file:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
                            myGrid.PageSize = 20;
                            myGrid.VirtualItemCount = GetNumItems();
            BindMyGrid();
        }
    }

    protected void MyGrid_PageIndexChanged(object sender, DataGridPageChangedEventArgs e)
    {
        myGrid.CurrentPageIndex = e.NewPageIndex;
        BindMyGrid();
    }

    private int GetNumItems()
    {
        return 500;
    }

    private void BindMyGrid()
    {
            Data[] array = GetDataFromInternetSomehow();
            this.myGrid.DataSource = array;
            this.myGrid.DataBind();
    }

    private class Data
    {
        public string Date { get; set; }
        public string Name { get; set; }
    }

Any thoughts on this would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

如歌彻婉言 2024-07-18 18:46:59

您的 ASPX 中存在错误:要连接 PageIndexChanged 事件处理程序,请使用属性 OnPageIndexChanged (不是代码中的 PageIndexChanged):

<asp:DataGrid ID="myGrid" runat="server"
   OnPageIndexChanged="MyGrid_PageIndexChanged"  /// <--- here's the error
   ...

然后,如果您有 AllowCustomPaging="true",您必须确保GetDataFromInternetSomehow()方法只会返回当前所选页面的数据,例如将当前页面传递给该方法并仅返回相应的数据:

GetDataFromInternetSomehow(e.NewPageIndex);

否则,禁用自定义分页它会正常工作(但每次都会加载所有数据)。

There is an error in your ASPX: to wire up the PageIndexChanged event handler use the property OnPageIndexChanged (not PageIndexChanged as in your code):

<asp:DataGrid ID="myGrid" runat="server"
   OnPageIndexChanged="MyGrid_PageIndexChanged"  /// <--- here's the error
   ...

Then, if you have AllowCustomPaging="true", you must ensure that the GetDataFromInternetSomehow() method will only return the data for the currently selected page, e.g. pass the current page to the method and return only the corresponding data:

GetDataFromInternetSomehow(e.NewPageIndex);

Otherwise, disable custom paging and it will just work (but all data will be loaded everytime).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文