DataGrid 分页 首页 上一页 下一页 跳转页

页面代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table id="Table1" align="center" border="1" cellpadding="1" cellspacing="1" style="font-size: 9pt"
            width="450">
            <tr>
                <td>
                    <asp:DataGrid ID="DataGrid1" runat="server" AllowPaging="True" PageSize="5" Width="100%" OnPageIndexChanged="DataGrid1_PageIndexChanged" ShowFooter="True">
                        <HeaderStyle Font-Size="9pt" />
                        <FooterStyle Font-Size="9pt" />
                        <PagerStyle Font-Size="9pt" Mode="NumericPages" />
                    </asp:DataGrid></td>
            </tr>
        </table>
        <table id="Table2" align="center" border="1" cellpadding="1" cellspacing="1" style="font-size: 9pt;
            width: 942px">
            <tr>
                <td>
                    <asp:LinkButton ID="LBtnFirst" runat="server" CommandName="First" OnClick="LBtnNavigation_Click" >首页</asp:LinkButton>
                    <asp:LinkButton ID="LBtnPrev" runat="server" CommandName="Prev"  OnClick="LBtnNavigation_Click" >上一页</asp:LinkButton>
                    <asp:LinkButton ID="LBtnNext" runat="server" CommandName="Next"  OnClick="LBtnNavigation_Click" >下一页</asp:LinkButton>
                    <asp:LinkButton ID="LBtnLast" runat="server" CommandName="Last"  OnClick="LBtnNavigation_Click" >尾页</asp:LinkButton>
                </td>
                <td>
                    第
                    <asp:Literal ID="LtlPageIndex" runat="server"></asp:Literal>页 共
                    <asp:Literal ID="LtlPageCount" runat="server"></asp:Literal>页 每页
                    <asp:Literal ID="LtlPageSize" runat="server"></asp:Literal>条 共
                    <asp:Literal ID="LtlRecordCount" runat="server"></asp:Literal>条
                     <asp:literal id="Literal1" runat="server" Text="转到第"></asp:literal><asp:textbox id="NewPageIndex" runat="server" Width="31px"></asp:textbox><asp:literal id="Literal2" runat="server" Text="页"></asp:literal>
                            <asp:button id="NewPageGo" runat="server" Text="Go" OnClick="NewPageGo_Click1"></asp:button>
                </td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>


后台代码:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Configuration;

public partial class Default2 : System.Web.UI.Page
{
    private static string connString = ConfigurationSettings.AppSettings["ConnString"];

    private int recordCount;

    private int pageCount;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            DataGridDataBind();

        }
    }
    private void DataGridDataBind()
    {

        DataSet ds = GetCustomersData();

        recordCount = ds.Tables[0].Rows.Count;

        //获取当前的页数

        pageCount = (int)Math.Ceiling(recordCount * 1.0 / PageSize);

        //避免纪录从有到无时,并且已经进行过反页的情况下CurrentPageIndex > PageCount出错

        if (recordCount == 0)
        {

            this.DataGrid1.CurrentPageIndex = 0;

        }

        else if (this.DataGrid1.CurrentPageIndex >= pageCount)
        {

            this.DataGrid1.CurrentPageIndex = pageCount - 1;

        }

        this.DataGrid1.DataSource = ds;

        this.DataGrid1.DataBind();

        NavigationStateChange();

    }

    public void LBtnNavigation_Click(object sender, System.EventArgs e)
    {

        LinkButton btn = (LinkButton)sender;

        switch (btn.CommandName)
        {

            case "First":

                PageIndex = 0;

                break;

            case "Prev"://if( PageIndex > 0 )

                PageIndex = PageIndex - 1;

                break;

            case "Next"://if( PageIndex < PageCount -1)

                PageIndex = PageIndex + 1;

                break;

            case "Last":

                PageIndex = PageCount - 1;

                break;

        }

        DataGridDataBind();

    }

    //数据绑定

    public static DataSet GetCustomersData()
    {
        connString = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
        SqlConnection conn = new SqlConnection(connString);
       
        string sqlStr = "SELECT CustomerID, CompanyName,Address,Phone FROM Customers";

        SqlCommand comm = new SqlCommand(sqlStr, conn);

        SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);

        DataSet ds = new DataSet();

        dataAdapter.Fill(ds);

        return ds;

    }

    //跳转
    private void NewPage_Go(string i)
    {
        try
        {
            DataGridDataBind();
            int PageIndex = Int32.Parse(i);
            if (PageIndex <= 0)
            {
                PageIndex = 0;
            }
            else
            {
                if (PageIndex > (int)Math.Ceiling(RecordCount*1.0 / PageSize))
                {
                    PageIndex = (int)Math.Ceiling(RecordCount*1.0/ PageSize);
                }
                else
                {
                    PageIndex--;
                }
            }
            //简单起见,将所有的linkbutton全部改为enable=true
            //FirstPage.Enabled = true;
            //NextPage.Enabled = true;
            //LastPage.Enabled = true;
            //PrevPage.Enabled = true;
            DataGrid1.CurrentPageIndex = PageIndex;
            DataGridDataBind();
           // ViewState["currentpage"] = PageIndex;
           // FillGrid(proc, (int)ViewState["currentpage"], PageSize, datagrid);
        }
        catch (Exception)
        {
            return;
        }
    }

    /// <summary>

    /// 控制导航按钮或数字的状态

    /// </summary>

    public void NavigationStateChange()
    {

        if (PageCount <= 1)//( RecordCount <= PageSize )//小于等于一页
        {

            this.LBtnFirst.Enabled = false;

            this.LBtnPrev.Enabled = false;

            this.LBtnNext.Enabled = false;

            this.LBtnLast.Enabled = false;

        }

        else //有多页
        {

            if (PageIndex == 0)//当前为第一页
            {

                this.LBtnFirst.Enabled = false;

                this.LBtnPrev.Enabled = false;

                this.LBtnNext.Enabled = true;

                this.LBtnLast.Enabled = true;

 

            }

            else if (PageIndex == PageCount - 1)//当前为最后页
            {

                this.LBtnFirst.Enabled = true;

                this.LBtnPrev.Enabled = true;

                this.LBtnNext.Enabled = false;

                this.LBtnLast.Enabled = false;

 

            }

            else //中间页
            {

                this.LBtnFirst.Enabled = true;

                this.LBtnPrev.Enabled = true;

                this.LBtnNext.Enabled = true;

                this.LBtnLast.Enabled = true;

            }

 

        }

        if (RecordCount == 0)//当没有纪录时DataGrid.PageCount会显示1页

            this.LtlPageCount.Text = "0";

        else

            this.LtlPageCount.Text = PageCount.ToString();

        if (RecordCount == 0)

            this.LtlPageIndex.Text = "0";

        else

            this.LtlPageIndex.Text = (PageIndex + 1).ToString();//在有页数的情况下前台显示页数加1

        this.LtlPageSize.Text = PageSize.ToString();

        this.LtlRecordCount.Text = RecordCount.ToString();

    }

 

 

    // 总页数

    public int PageCount
    {

        get { return this.DataGrid1.PageCount; }

    }

    //页大小

    public int PageSize
    {

        get { return this.DataGrid1.PageSize; }

    }

    //页索引,从零开始

    public int PageIndex
    {

        get { return this.DataGrid1.CurrentPageIndex; }

        set { this.DataGrid1.CurrentPageIndex = value; }

    }

    // 纪录总数

    public int RecordCount
    {

        get { return recordCount; }

        set { recordCount = value; }

    }

    protected void LBtnFirst_Click(object sender, EventArgs e)
    {
        NavigationStateChange();
    }
    protected void LBtnPrev_Click(object sender, EventArgs e)
    {
        NavigationStateChange();
    }
    protected void LBtnNext_Click(object sender, EventArgs e)
    {
        NavigationStateChange();
    }
    protected void LBtnLast_Click(object sender, EventArgs e)
    {
        NavigationStateChange();
    }
    protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        //LoSubSetData();
        DataGrid1.CurrentPageIndex = e.NewPageIndex;
        DataGridDataBind();
    }
    protected void NewPageGo_Click1(object sender, EventArgs e)
    {
        NewPage_Go(NewPageIndex.Text.Trim());
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值