asp.net 网格视图分页在刷新之前不显示

本文关键字:显示 刷新 net 网格 视图 分页 asp | 更新日期: 2023-09-27 18:23:45

我在页面上有一个DataGridView。当我调用该页面时,整个列表都来了。我决定在GridView中添加分页。

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" OnRowDeleting="GridView1_Del" OnSelectedIndexChanging="GridView1_Sel"  OnPageIndexChanging="GridView1_PageIndexChanging">
            <Columns>
                <asp:BoundField DataField="id" HeaderText="Id" SortExpression="id" Visible="false" />
                <asp:BoundField DataField="name" HeaderText="Name" SortExpression="name" />
                <asp:BoundField DataField="author" HeaderText="Author" SortExpression="author" />
                <asp:BoundField DataField="active" HeaderText="Active" SortExpression="active" />
                <asp:CommandField HeaderText="Delete" SelectText="Delete"  ShowDeleteButton="True" ButtonType="Button" />
            </Columns>
        </asp:GridView>

//Page_Load()
gridFill();
GridView1.AllowPaging = true;
GridView1.PageSize = 10;

gridFill()方法填充GridView

public void gridFill()
{
    conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:/inetpub/example.com/db/db.mdb");
    sql = "SELECT  id, name, author, active, FROM [table]";
    dt = new DataTable();
    try
    {
        if (conn.State != ConnectionState.Open) conn.Open();
        comm= new OleDbCommand(sql, conn);
        da= new OleDbDataAdapter(comm);
        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
    catch (System.Data.OleDb.OleDbException ex)
    {
        string msg = "Error: ";
        msg += ex.Message;
        throw new Exception(msg);
    }
    finally
    {
        conn.Close();
    }
}

当我调用页面时,它仍然一次获取所有行。此外,如果我单击第 11 行或更晚(因为我将分页限制为 10 行(进行删除,我会收到索引错误。

所以我添加了另一个标记为"刷新"的Button,它再次调用 gridFill(( 方法。然后分页生效。

GridView 第一次不分页的原因可能是什么?

asp.net 网格视图分页在刷新之前不显示

如果我更改行的顺序

//Page_Load()
gridFill();
GridView1.AllowPaging = true;
GridView1.PageSize = 10;

//Page_Load()
GridView1.AllowPaging = true;
GridView1.PageSize = 10;
gridFill();

它有效。我真不敢相信