asp.net 分页控件

8/10/2015来源:ASP.NET技巧人气:1272

asp.net 分页控件

前台

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Pager.ascx.cs" Inherits="Controls_Core_Pager" %><style type="text/CSS">    .GridViewPagerStyle {        background-position: top;        background-repeat: repeat-x;        border-bottom: 1px solid #ABC7EC;        color: #15428B;        font-weight: bold;    }    .GridViewPagerStyle table {        margin: auto;        text-align: center;    }    .GridViewPagerStyle table td { border: 0px; }    .GridViewPagerStyle a {        color: #15428B;        padding: 0px 1px 0px 1px;        text-decoration: none;    }    .GridViewPagerStyle span {        color: red;        font-weight: bold;        padding: 0px 1px 0px 1px;        text-decoration: none;    }</style><script type="text/javascript" language="Javascript">    function checkGo() {        var txtGoObj = document.getElementById(document.getElementById('hidTextBoxGoClientID').value);        if (txtGoObj && txtGoObj.value == '') {            txtGoObj.focus();            return false;        }        return true;    }</script><center>    <div style="background-color: #BBD5EC; width: 100%;">        <table class="GridViewPagerStyle" cellpadding="1" cellspacing="1" >            <tr align="center">                <td valign="middle" runat="server" id="tdAll" style="display: none;">                    <asp:LinkButton ID="imgAll" Text="显示所有" runat="server" OnClick="All_Click" />|                </td>                <td valign="middle" runat="server" id="tdPageCount">                    共<asp:Label ID="lblPageCount" runat="server" Text="Label" Style="color: Red;"></asp:Label>页/<asp:Label                                                                                                                    ID="lblRecordCount" runat="server" Text="Label" Style="color: Red;"></asp:Label>条记录|                </td>                <td valign="middle" runat="server" id="tdCurrentIndex">                    当前第<asp:Label ID="lblCurrentPageIndex" runat="server" Text="Label" Style="color: Red;"></asp:Label>页                </td>                <td valign="middle">                    <asp:LinkButton ID="imgFirstPage" runat="server" OnClick="imgFirstPage_Click" Text="首页"></asp:LinkButton>                </td>                <td valign="middle">                    <asp:LinkButton ID="imgPRePage" runat="server" OnClick="imgPrePage_Click" Text="上页"></asp:LinkButton>                </td>                <td valign="middle">                    <asp:LinkButton ID="imgNextPage" runat="server" OnClick="imgNextPage_Click" Text="下页"></asp:LinkButton>                </td>                <td valign="middle">                    <asp:LinkButton ID="imgLastPage" runat="server" OnClick="imgLastPage_Click" Text="尾页"></asp:LinkButton>                </td>                <td>                </td>                <td valign="middle" runat="server" id="tdTextGo">                    转到<asp:TextBox ID="txtGo" runat="server" Width="20px" Height="12" onkeyup="this.value=this.value.replace(/\D/g,'')"                                   onafterpaste="this.value=this.value.replace(/\D/g,'')"></asp:TextBox>页                </td>                <td valign="middle" runat="server" id="tdImgGo" align="center">                    <asp:ImageButton ID="imgGo" AlternateText="跳转页面" ImageUrl="../../Images/Go.gif" OnClick="imgGo_Click"                                     ImageAlign="Middle" runat="server" Height="21px" Width="28px" OnClientClick=" return checkGo(); " />                </td>            </tr>        </table>    </div></center><asp:HiddenField ID="hidCurrentPageIndex" runat="server" /><asp:HiddenField ID="hidPageSize" runat="server" />

后台:

public partial class Controls_Core_Pager : System.Web.UI.UserControl{    public delegate void GridViewDelegate();    #region Delegate    public event GridViewDelegate InitLoadData;    public event GridViewDelegate PageingLoadData;    public event GridViewDelegate BeforePageing;    public event GridViewDelegate AfterPageing;    private const string firstenabledimgurl = "~/Image/pager/first.gif";    private const string firstdisabledimgurl = "~/image/pager/first_disabled.gif";    private const string lastenabledimgurl = "~/image/pager/last.gif";    private const string lastdisabledimgurl = "~/image/pager/last_disabled.gif";    private const string prevenabledimgurl = "~/image/pager/prev.gif";    private const string prevdisabledimgurl = "~/image/pager/prev_disabled.gif";    private const string nextenabledimgurl = "~/image/pager/next.gif";    private const string nextdisabledimgurl = "~/image/pager/next_disabled.gif";    #endregion    public bool SimplePager    {        set        {            this.tdAll.Visible = !value;            this.tdCurrentIndex.Visible = !value;            this.tdImgGo.Visible = !value;            this.tdPageCount.Visible = !value;            this.tdTextGo.Visible = !value;        }    }    protected void Page_Load(object sender, EventArgs e)    {    }    public int RecordCount    {        get { return (ViewState["RecordCount"] != null) ? (int)ViewState["RecordCount"] : 0; }        set { ViewState["RecordCount"] = value; }    }    public int PageSize    {        get        {            if (string.IsNullOrEmpty(hidPageSize.Value) ||Convert.ToInt32( hidPageSize.Value)<=0)                hidPageSize.Value = "20";            return int.Parse(hidPageSize.Value);        }        set        {            hidPageSize.Value = value.ToString();        }    }    public int CurrentPageIndex    {        get { return (hidCurrentPageIndex.Value == string.Empty) ? 0 : int.Parse(hidCurrentPageIndex.Value); }        set { hidCurrentPageIndex.Value = value.ToString(); }    }    public void LoadData()    {        if (InitLoadData != null)        {            InitLoadData();            ShowStatus(0);        }    }    public void ReLoadData()    {        if (BeforePageing != null)        {            BeforePageing();        }        if (PageingLoadData != null)        {            PageingLoadData();            ShowStatus(CurrentPageIndex);        }        if (AfterPageing != null)        {            AfterPageing();        }    }    private void ShowStatus(int currentPageIndex)    {        int pageCount = Convert.ToInt32((RecordCount - 1) / PageSize + 1);        lblRecordCount.Text = RecordCount.ToString();        hidCurrentPageIndex.Value = currentPageIndex.ToString();        if (RecordCount == 0)        {            lblPageCount.Text = "0";            lblCurrentPageIndex.Text = "";        }        else        {            lblPageCount.Text = pageCount.ToString();            lblCurrentPageIndex.Text = Convert.ToString(currentPageIndex + 1);        }        if (pageCount == 0 || ((currentPageIndex + 1) == 1 && pageCount == 1))        {            imgFirstPage.Enabled = false;            imgPrePage.Enabled = false;            imgNextPage.Enabled = false;            imgLastPage.Enabled = false;        }        else        {            if (currentPageIndex == 0)            {                imgPrePage.Enabled = false;                imgFirstPage.Enabled = false;                imgNextPage.Enabled = true;                imgLastPage.Enabled = true;            }            else if ((currentPageIndex + 1) == pageCount)            {                imgFirstPage.Enabled = true;                imgPrePage.Enabled = true;                imgNextPage.Enabled = false;                imgLastPage.Enabled = false;            }            else if (currentPageIndex != 0 && (currentPageIndex + 1) != pageCount)            {                imgFirstPage.Enabled = true;                imgPrePage.Enabled = true;                imgNextPage.Enabled = true;                imgLastPag