ASP.NET DataGrid 和自定义分页 [英] ASP.NET DataGrid and custom paging

查看:21
本文介绍了ASP.NET DataGrid 和自定义分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

当我查看页面时,我看到网格中的第一组结果,上一个链接被禁用.但是,当我单击下一步"时,我再次看到网格的第一页禁用了上一个链接.在调试代码时,我确定 MyGrid_PageIndexChanged() 事件处理程序从未被调用.

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.

在 ASPX 文件中:

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>

在 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; }
    }

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

Any thoughts on this would be much appreciated.

推荐答案

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

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

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

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

这篇关于ASP.NET DataGrid 和自定义分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆