ASP.NET GridView自定义分页最终页面问题

时间:2014-10-27 14:46:11

标签: c# asp.net gridview webforms

我试图在GridView上实现自定义分页,但在显示最后一页时会出现奇怪的行为。我认为这可能是因为我通过扩展4.0 GridView控件手动实现它,但我现在尝试按照http://www.c-sharpcorner.com/UploadFile/99bb20/custom-paging-with-gridview-control-in-Asp-Net-4-5/使用4.5功能,但它显示完全相同的行为。

基本上,如果最后一页的项目数小于PageSize,那么无论何时网格回发,它都会填满空行,如下所示:

页面标记:

<asp:GridView ID="gvTest" runat="server" PageSize="10" AllowPaging="true" AllowCustomPaging="true" AutoGenerateColumns="false" OnRowCommand="gvTest_RowCommand" OnPageIndexChanging="gvTest_PageIndexChanging">
    <Columns>
        <asp:BoundField DataField="ItemText" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="btnTest" runat="server" Text="Test postback" CommandName="TEST" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

代码隐藏:

protected void Page_Load(object sender, EventArgs e) {
    if (!IsPostBack) {
        BindData();
    }
}

private void BindData() {
    List<TestItem> items = new List<TestItem>() {
        new TestItem() { ItemText = "Item1" },
        new TestItem() { ItemText = "Item2" },
        new TestItem() { ItemText = "Item3" },
        new TestItem() { ItemText = "Item4" },
        new TestItem() { ItemText = "Item5" },
        new TestItem() { ItemText = "Item6" },
        new TestItem() { ItemText = "Item7" },
        new TestItem() { ItemText = "Item8" },
        new TestItem() { ItemText = "Item9" },
        new TestItem() { ItemText = "Item10" },
        new TestItem() { ItemText = "Item11" },
        new TestItem() { ItemText = "Item12" }
    };

    gvTest.DataSource = items.Skip(gvTest.PageIndex * gvTest.PageSize).Take(gvTest.PageSize).ToList();
    gvTest.VirtualItemCount = items.Count;
    gvTest.DataBind();
}

protected void gvTest_PageIndexChanging(object sender, GridViewPageEventArgs e) {
    gvTest.PageIndex = e.NewPageIndex;
    BindData();
}

所有工作都按预期工作,但点击第二(最后)页面上的链接按钮会产生以下结果:

回发前:

Before postback

回发后:

After postback

这是一个已知的GridView问题吗?不确定我做错了什么......

2 个答案:

答案 0 :(得分:0)

哈哈哈,刚才注意到这是一个相对古老的问题。希望它有助于某人!

它与您的OnRowCommand有关吗?我取出了OnRowCommand,因为我不知道你在那个街区做了什么。否则代码工作正常

我运行了以下代码:

aspx页面:

<asp:GridView ID="gvTest" runat="server" PageSize="10" AllowPaging="true" AllowCustomPaging="true" AutoGenerateColumns="false" OnPageIndexChanging="gvTest_PageIndexChanging">
        <Columns>
            <asp:BoundField DataField="ItemText" />
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton ID="btnTest" runat="server" Text="Test postback" CommandName="TEST" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

使用模拟类后面的代码:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            List<TestItem> items = new List<TestItem>()
            {
                new TestItem() {ItemText = "Item1"},
                new TestItem() {ItemText = "Item2"},
                new TestItem() {ItemText = "Item3"},
                new TestItem() {ItemText = "Item4"},
                new TestItem() {ItemText = "Item5"},
                new TestItem() {ItemText = "Item6"},
                new TestItem() {ItemText = "Item7"},
                new TestItem() {ItemText = "Item8"},
                new TestItem() {ItemText = "Item9"},
                new TestItem() {ItemText = "Item10"},
                new TestItem() {ItemText = "Item11"},
                new TestItem() {ItemText = "Item12"}
            };

            gvTest.DataSource = items.Skip(gvTest.PageIndex * gvTest.PageSize).Take(gvTest.PageSize).ToList();
            gvTest.VirtualItemCount = items.Count;
            gvTest.DataBind();
        }

        protected void gvTest_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvTest.PageIndex = e.NewPageIndex;
            BindData();
        }
    }

    public class TestItem
    {
        public string ItemText { get; set; }
    }

导致:

第1页:enter image description here 第2页:http://i.imgur.com/UPc7Rq6.jpg

答案 1 :(得分:0)

我用这种方法来避免这个问题

但这不是一个好方法......

_y