如何在DataGrid中实现分页功能

本文展示了一个使用ASP.NET DataGrid实现分页功能的示例,包括前后端代码实现细节,如页面跳转按钮功能、数据绑定过程及异常处理。

 DataGridPager.aspx

 

<%@ Page language="c#" Codebehind="DataGridPager.aspx.cs" AutoEventWireup="false" Inherits="CommonFunction.DataGridPager" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    
<HEAD>
        
<meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
        
<meta content="JavaScript" name="vs_defaultClientScript">
        
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    
</HEAD>
    
<body MS_POSITIONING="GridLayout">
        
<form id="Form1" runat="server">
            
<TABLE id="Table1" style="Z-INDEX: 101; LEFT: 32px; WIDTH: 480px; POSITION: absolute; TOP: 24px; HEIGHT: 279px"
                cellSpacing
="1" cellPadding="1" width="480" border="0">
                
<tr>
                    
<td align="center">
                        
<h2>DataGrid分页的例子</h2>
                    
</td>
                
</tr>
                
<TR>
                    
<TD style="HEIGHT: 206px" vAlign="top"><asp:datagrid id="DataGridPage" runat="server" GridLines="Horizontal" BackColor="White" BorderStyle="None"
                            Width
="480px" AllowPaging="True" PageSize="5" PagerStyle-Mode="NumericPages" PagerStyle-HorizontalAlign="Right" BorderColor="#E7E7FF"
                            BorderWidth
="1px" CellPadding="3" Font-Name="Verdana" HeaderStyle-BackColor="#aaaadd" AlternatingItemStyle-BackColor="#eeeeee"
                            HorizontalAlign
="Center" AutoGenerateColumns="False">
                            
<SelectedItemStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#738A9C"></SelectedItemStyle>
                            
<AlternatingItemStyle BackColor="#F7F7F7"></AlternatingItemStyle>
                            
<ItemStyle ForeColor="#4A3C8C" BackColor="#E7E7FF"></ItemStyle>
                            
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" ForeColor="#F7F7F7" BackColor="#4A3C8C"></HeaderStyle>
                            
<FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE"></FooterStyle>
                            
<Columns>
                                
<asp:BoundColumn DataField="lastname" HeaderText="lastname">
                                    
<HeaderStyle Width="480px"></HeaderStyle>
                                
</asp:BoundColumn>
                                
<asp:BoundColumn DataField="firstname" HeaderText="firstname"></asp:BoundColumn>
                            
</Columns>
                            
<PagerStyle HorizontalAlign="Right" ForeColor="#4A3C8C" BackColor="#E7E7FF" Mode="NumericPages"></PagerStyle>
                        
</asp:datagrid></TD>
                
</TR>
                
<TR>
                    
<TD align="right"><asp:label id="lblPageCount" runat="server"></asp:label><asp:label id="lblCurrentIndex" runat="server"></asp:label><asp:linkbutton id="btnFirst" onclick="PageButtonClick" runat="server" Font-Name="verdana" CommandArgument="0">最首页</asp:linkbutton><asp:linkbutton id="btnPrev" onclick="PageButtonClick" runat="server" CommandArgument="prev">前一页</asp:linkbutton><asp:linkbutton id="btnNext" onclick="PageButtonClick" runat="server" CommandArgument="next">下一页</asp:linkbutton><asp:linkbutton id="btnLast" onclick="PageButtonClick" runat="server" CommandArgument="last">最后页</asp:linkbutton></TD>
                
</TR>
            
</TABLE>
        
</form>
    
</body>
</HTML>

 

 

DataGridPager.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
namespace CommonFunction
{
    
/// <summary>
    
/// testDataGridPager 的摘要说明。
    
/// </summary>

    public class DataGridPager : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.Label lblPageCount;
        
protected System.Web.UI.WebControls.Label lblCurrentIndex;
        
protected System.Web.UI.WebControls.LinkButton btnFirst;
        
protected System.Web.UI.WebControls.LinkButton btnPrev;
        
protected System.Web.UI.WebControls.LinkButton btnNext;
        
protected System.Web.UI.WebControls.DataGrid DataGridPage;
        
protected System.Web.UI.WebControls.LinkButton btnLast;
        
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
//页面初试化时进行数据绑定
            if(!IsPostBack)
                BindGrid();
        }

        
//显示当前分页信息
        private void ShowStats()
        
{
            
//显示当前页面是第几页
            lblCurrentIndex.Text = "第 " + (DataGridPage.CurrentPageIndex + 1).ToString() + " 页";
            
//显示总页数
            lblPageCount.Text = "总共 " + DataGridPage.PageCount.ToString() + " 页";
        }


        
Web 窗体设计器生成的代码
        
//分别处理”最首页“、“前一页”、“下一页”和“最后页”四个按钮单击时设置DataGrid控件的当前页的索引
        public void PageButtonClick(object sender, EventArgs e)
        
{
            
//取得按钮单击时传递的命令参数
            string arg = ((LinkButton)sender).CommandArgument.ToString();
            
switch(arg)
            
{
                
//如果点击的是“下一页”
                case "next":
                    
//如果当前页不是最后一页
                    if (DataGridPage.CurrentPageIndex < (DataGridPage.PageCount - 1))
                    
{
                        
//设置DataGrid控件的当前页索引为下一页面
                        DataGridPage.CurrentPageIndex += 1;
                    }

                    
break;
                
//如果点击的是“前一页”
                case "prev":
                    
//如果当前页不是首页
                    if (DataGridPage.CurrentPageIndex > 0)
                    
{
                        
//设置DataGrid控件的当前页索引为上一页面
                        DataGridPage.CurrentPageIndex -= 1;
                    }

                    
break;
                
//如果点击的是“最后页”
                case "last":
                    
//设置当前页的索引为最后一页
                    DataGridPage.CurrentPageIndex = (DataGridPage.PageCount - 1);
                    
break;
                
//默认为”最首页“
                default:
                    
//设置当前页的索引为首页
                    DataGridPage.CurrentPageIndex = System.Convert.ToInt32(arg);
                    
break;
            }

            BindGrid();
            ShowStats();
        }


        
private void BindGrid()
        
{
            
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
            SqlConnection cnn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
            
//创建数据适配器对象
            SqlDataAdapter da = new SqlDataAdapter("select employeeid,lastname,firstname from employees",cnn);
            
//创建DataSet对象
            DataSet ds = new DataSet();
            
try
            
{
                
//填充数据集
                da.Fill(ds, "testTable");
                
//进行数据绑定
                DataGridPage.DataSource = ds;
                DataGridPage.DataBind();
            }

            
catch(Exception error)
            
{
                Response.Write(error.ToString());
            }
        
        }


        
public void DataGridPage_Page(object sender, DataGridPageChangedEventArgs e)
        
{
            
//设置DataGrid当前页的索引值为用户选择的页的索引
            DataGridPage.CurrentPageIndex = e.NewPageIndex;
            
//重新绑定数据
            BindGrid();
            
//显示当前分页信息
            ShowStats();
        }

    }

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值