Sorting and Paging of GridView with Model Binding

Recently we got a bug from customer said that GridView sorting is not work well with paging enabled when using Model Binding. The "bug" behavior is different from when using an ObjectDataSource to populate the GridView, so bug was reported.

Let us see the "bug" detail first:

Default.aspx

    <asp:GridView ID="userGridView" runat="server" AllowPaging="true" AllowSorting="true" AutoGenerateColumns="false"
        DataKeyNames="Id" SelectMethod="userGridView_GetData">
        <Columns>
            <asp:BoundField DataField="LastName" HeaderText="Last Name" SortExpression="LastName" />
            <asp:BoundField DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" />
        </Columns>
    </asp:GridView>

Default.aspx.cs

namespace GridViewSort
{
    public class User
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public IEnumerable<User> GetUsers()
        {
            User[] users = new[]
            {
               new User { Id = 1, FirstName = "A", LastName = "One" },
               new User { Id = 2, FirstName = "B", LastName = "Two" },
               new User { Id = 3, FirstName = "C", LastName = "Three" },
               new User { Id = 4, FirstName = "D", LastName = "Four" },
               new User { Id = 5, FirstName = "E", LastName = "Five" },
               new User { Id = 6, FirstName = "F", LastName = "Six" }
            };

            return users.OrderByDescending(u => u.LastName);
        }

        public IQueryable<User> userGridView_GetData()
        {
            var users = GetUsers();

            return users.AsQueryable();
        }
    }
}

The GetUsers() returns the collection ordered by User LastName. When setAllowPaging ="false", GridView render as expected(rows ordered by LastName, even DataKeyNames="Id"), like this:

image.png

However, when set AllowPaging ="true", behavior changes, GridView rows not sorted by LastName anymore. like this:

image.png

Then the "bug" was report, AllowPaging ="true" leads the behavior change. Customer cannot get an ordered GridView in the page initialize.

After our investigation, this is not a bug actually, ASP.NET model binding already provide a way to handle such scenario. The solution is update the SelectMethod to
public IQueryable<User> userGridView_GetData(int maximumRows, int startRowIndex, out int totalRowCount, string sortByExpression)
instead of
public IQueryable<User> userGridView_GetData()

After some small change, the behavior must meet the customer's expected:

public IQueryable<User> userGridView_GetData(int maximumRows, int startRowIndex, out int totalRowCount, string sortByExpression)
        {
            var users = GetUsers();
            totalRowCount = users.Count();
            return users.Skip(startRowIndex).Take(maximumRows).AsQueryable();
        }

Then the result will be


image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的阅读 13,550评论 5 6
  • 人的精力终究是有限的,不可能做到所有想做到的事,终究会失去一些东西,即使不想放弃。当你同时想要得到很多东西的时候,...
    放羊的小娃娃阅读 223评论 0 0
  • 冥想,群里很多小伙伴还参加了主题为冥想的打卡群。我却怎么也沉不下心来,冥想时脑海里总是有各种思绪乱窜,今天来乱窜...
    童小咪阅读 176评论 0 1
  • 昨天晚上睡的迟了点,又做梦了,前天梦到自己怀孕了和一个人在说话,然后就是吵闹,最后归于平静在嬉笑说话,看不清楚那个...
    遇见继承阅读 154评论 0 0
  • 作者:zengzhiyan 来源:网易用户体验中心 一边是产品的商业需求,它可以改善产品的市场表现,为企业带来利润...
    Sting阅读 771评论 0 1