ASP.NET DataGrid分页异常:点击分页始终显示第一页数据
解决ASP.NET DataGrid始终显示第一页的分页问题
兄弟,我看了你的代码,发现几个关键问题导致分页无效,咱们一步步来修复:
问题根源分析
- 重复注册分页事件:你在
BindGrid方法里每次绑定都注册PageIndexChanged事件,加上前端已经通过OnPageIndexChanged指定了事件处理方法,这会导致事件逻辑混乱,甚至重复执行。 - 自定义分页未做数据筛选:因为你开启了
AllowCustomPaging="True",DataGrid不会自动帮你分页,你得自己根据当前页码从数据库获取对应页的数据。但你现在的查询是SELECT * FROM dbo.TestingLatihan,每次都返回全部数据,自然只会显示前5条(第一页)。 - 数据库连接未正确释放:你的代码里直接
con.Close(),如果中间抛出异常,连接可能不会关闭,最好用using语句自动释放资源。
修改后的后台代码
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindGrid(); } } private int GetNumItems() { // 使用using自动释放连接,避免资源泄漏 using (SqlConnection con = new SqlConnection(gc.GetWebConfigConnectionStringAIS())) { con.Open(); string query = "SELECT COUNT(*) FROM dbo.TestingLatihan"; using (SqlCommand cmd = new SqlCommand(query, con)) { return (Int32)cmd.ExecuteScalar(); } } } protected void grid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e) { grid1.CurrentPageIndex = e.NewPageIndex; BindGrid(); } private void BindGrid() { grid1.VirtualItemCount = GetNumItems(); // 计算当前页的起始行索引 int startRow = grid1.CurrentPageIndex * grid1.PageSize; using (SqlConnection con = new SqlConnection(gc.GetWebConfigConnectionStringAIS())) { con.Open(); // 使用OFFSET FETCH实现分页(SQL Server 2012及以上版本支持) string query = @"SELECT * FROM dbo.TestingLatihan ORDER BY ID_ -- 必须排序,OFFSET需要依赖有序数据集 OFFSET @StartRow ROWS FETCH NEXT @PageSize ROWS ONLY"; using (SqlCommand cmd = new SqlCommand(query, con)) { // 添加分页参数,避免SQL注入 cmd.Parameters.AddWithValue("@StartRow", startRow); cmd.Parameters.AddWithValue("@PageSize", grid1.PageSize); SqlDataAdapter myAdapter = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); myAdapter.Fill(dt); grid1.DataSource = dt; grid1.DataBind(); } } }
前端代码调整(可选优化)
你的前端代码基本没问题,去掉默认开启的EnableViewState="true"(无需显式声明),确保事件绑定名称和后台一致即可:
<asp:DataGrid ID="grid1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" Width="100%" OnItemCommand="dtgItemCommand" AllowPaging="True" PageSize="5" AllowCustomPaging="True" OnPageIndexChanged="grid1_PageIndexChanged"> <AlternatingItemStyle CssClass="tdgenap" /> <ItemStyle CssClass="tdganjil" HorizontalAlign="Center" /> <HeaderStyle HorizontalAlign="Center" Height="30px" CssClass="tdjudul"></HeaderStyle> <Columns> <asp:TemplateColumn HeaderText="ID" > <HeaderStyle Font-Underline="false" Height="15px" Width="5%" HorizontalAlign="Center" BackColor="#ccffcc"></HeaderStyle> <ItemStyle HorizontalAlign="Center"></ItemStyle> <ItemTemplate> <asp:Label id="lblID" runat="server" text='<%#DataBinder.Eval(Container.DataItem, "ID_")%>'></asp:Label> </ItemTemplate> </asp:TemplateColumn> </Columns> <PagerStyle Font-Bold="True" ForeColor="black" HorizontalAlign="Center" Wrap="True" Mode="NumericPages" /> <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> </asp:DataGrid>
关键说明
- 分页查询必须排序:
OFFSET FETCH要求结果集是有序的,所以一定要添加ORDER BY语句,这里用ID_排序,你可以根据需求换成其他字段。 - 自定义分页核心逻辑:通过
CurrentPageIndex * PageSize计算起始行,只从数据库获取当前页的数据,而不是一次性拉取全部数据,既提升性能也解决了分页无效的问题。 - 资源安全释放:用
using包裹数据库连接和命令对象,确保即使发生异常,资源也能被自动释放,避免连接池耗尽。
这样修改后,点击分页按钮应该就能正常切换页面了!
内容的提问来源于stack exchange,提问作者illimite
相关产品推荐
相关产品推荐

