首页  ·  知识 ·  云计算
ASP.NET简单分页
Jasonlee's Blog  http://www.jasonlee.cn/article.asp?id=77  综合  编辑:dezai  图片来源:网络
usingSystem;///summary///PageClass的摘要说明////summarypublicclassPageC
using System;

/// 
/// PageClass 的摘要说明
/// 

public class PageClass
{
    #region 数字分页类
    public static string strPage(int intCounts, int intPageSizes, int intPageCounts, int intThisPages, string strUrl)
    {
        int intCount = Convert.ToInt32(intCounts); //总记录数
        int intPageCount = Convert.ToInt32(intPageCounts); //总共页数
        int intPageSize = Convert.ToInt32(intPageSizes); //每页显示
        int intPage = 7;  //数字显示
        int intThisPage = Convert.ToInt32(intThisPages); //当前页数
        int intBeginPage = 0; //开始页数
        int intCrossPage = 0; //变换页数
        int intEndPage = 0; //结束页数
        string strPage = null; //返回值

        intCrossPage = intPage / 2;
        strPage = "共 " + intCount.ToString() + " 条记录 第 " + intThisPage.ToString() + "/" + intPageCount.ToString() + " 页 每页 " + intPageSize.ToString() + " 条     ";
        if (intThisPage > 1)
        {
            strPage = strPage + " ";
            strPage = strPage + " ";
        }
        if (intPageCount > intPage)
        {
            if (intThisPage > intPageCount - intCrossPage)
            {
                intBeginPage = intPageCount - intPage + 1;
                intEndPage = intPageCount;
            }
            else
            {
                if (intThisPage <= intPage - intCrossPage)
                {
                    intBeginPage = 1;
                    intEndPage = intPage;
                }
                else
                {
                    intBeginPage = intThisPage - intCrossPage;
                    intEndPage = intThisPage + intCrossPage;
                }
            }
        }
        else
        {
            intBeginPage = 1;
            intEndPage = intPageCount;
        }
        if (intCount > 0)
        {

            for (int i = intBeginPage; i <= intEndPage; i++)
            {
                if (i == intThisPage)
                {
                    strPage = strPage + " " + i.ToString() + " ";
                }
                else
                {
                    strPage = strPage + " " + i.ToString() + " ";
                }
            }
        }
        if (intThisPage < intPageCount)
        {
            strPage = strPage + " ";
            strPage = strPage + " ";
        }
        strPage = strPage + " 跳转到第  页  ";
        return strPage;
    }
    #endregion
}

ASP.NET页面:(Default.aspx)
using System;
using System.Data;
using System.Configuration;
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;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            GetDate(Request.QueryString["KeyWord"]);
        }
    }

    protected void imgSearch_Click(object sender, ImageClickEventArgs e)
    {
        if (this.KeyWord.Text.Length > 0)
        {
            Response.Redirect("Default.aspx?KeyWord=" + this.KeyWord.Text);
        }
        else
        {
            Response.Redirect("Default.aspx");
        }
    }

    protected void GetDate(string Key)
    {
        int CurPage, intRowCount;
        string strKeyWord = null;
        string strUrl = null;
        if (Key != null)
        {
            strKeyWord = Key;
            strUrl = "Default.aspx?KeyWord=" + strKeyWord + "&Page=";
        }
        else
        {
            strKeyWord = "";
            strUrl = "Default.aspx?Page=";
        }
        ProductsBLL ProductsLogic = new ProductsBLL();
        PagedDataSource Products = new PagedDataSource();
        Products.DataSource = ProductsLogic.GetProductByProductName(strKeyWord).DefaultView;
        intRowCount = ProductsLogic.GetProductByProductName(strKeyWord).Rows.Count;
        Products.AllowPaging = true;
        Products.PageSize = 10;
        if (Request.QueryString["Page"] != null)
        {
            CurPage = Convert.ToInt32(Request.QueryString["Page"]);
        }
        else
        {
            CurPage = 1;
        }
        Products.CurrentPageIndex = CurPage - 1;
        this.textPage.Text = PageClass.strPage(intRowCount, Products.PageSize, Products.PageCount, CurPage, strUrl);
        this.RepeaterUser.DataSource = Products;
        this.RepeaterUser.DataBind();
        if (intRowCount < 1)
        {
            Response.Write("
提示:没有找到任何数据.
");
            this.PanelTable.Visible = false;
        }
    }
}
本文作者:Jasonlee's Blog 来源:http://www.jasonlee.cn/article.asp?id=77
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读