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

查看:128
本文介绍了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.

当我浏览网页我看到的第一套网格中的结果,在禁用previous链接。当我点击下一步不过,我再次看到禁用previous链接网格的第一页。当调试code我查明该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.

我包括我下面的简化code。我已经改变了变量名和省略了方法,把重点放在数据网格分页问题。

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

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

任何对此的思考将是非常美联社preciated。

Any thoughts on this would be much appreciated.

推荐答案

有在你的ASPX一个错误:要连接的PageIndexChanged事件处理程序使用属性的 OnPageIndexChanged (不是 PageIndexChanged 作为在code):

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 =真正的上,您必须确保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天全站免登陆