gpt4 book ai didi

c# - ASP.NET GridView自定义分页最后一页问题

转载 作者:太空宇宙 更新时间:2023-11-03 15:49:26 25 4
gpt4 key购买 nike

我正在尝试在 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 问题吗?不确定我做错了什么....

最佳答案

哈哈哈,才发现这是一个比较老的问题。希望它对某人有所帮助!

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

我运行了以下代码:

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

关于c# - ASP.NET GridView自定义分页最后一页问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26590179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com