• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
内蒙古峰回路转armyfeng
博客园    首页    新随笔    联系   管理    订阅  订阅

ASP.NET简单分页

运行效果:


数字分页类:(PageClass.cs)
using System;

/// <summary>
/// PageClass 的摘要说明
/// </summary>
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 = "共 <font color=\"#FF0000\">" + intCount.ToString() + "</font> 条记录 第 <font color=\"#FF0000\">" + intThisPage.ToString() + "/" + intPageCount.ToString() + "</font> 页 每页 <font color=\"#FF0000\">" + intPageSize.ToString() + "</font> 条 &nbsp;&nbsp;&nbsp;&nbsp;";
        if (intThisPage > 1)
        {
            strPage = strPage + "<a href=\"" + strUrl + "1\"><img src=\"App_Themes/Image/icon_page_01.gif\" alt=\"最前一页\" width=\"21px\" height=\"20px\" border=\"0\" align=\"absmiddle\" /></a> ";
            strPage = strPage + "<a href=\"" + strUrl + Convert.ToString(intThisPage - 1) + "\"><img src=\"App_Themes/Image/icon_page_02.gif\" alt=\"上一页\" width=\"21px\" height=\"20px\" border=\"0\" align=\"absmiddle\" /></a> ";
        }
        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 + " <font color=\"#FF0000\">" + i.ToString() + "</font> ";
                }
                else
                {
                    strPage = strPage + " <a href=\"" + strUrl + i.ToString() + "\" title=\"第" + i.ToString() + "页\">" + i.ToString() + "</a> ";
                }
            }
        }
        if (intThisPage < intPageCount)
        {
            strPage = strPage + "<a href=\"" + strUrl + Convert.ToString(intThisPage + 1) + "\"><img src=\"App_Themes/Image/icon_page_03.gif\" alt=\"下一页\" width=\"21px\" height=\"20px\" border=\"0\" align=\"absmiddle\" /></a> ";
            strPage = strPage + "<a href=\"" + strUrl + intPageCount.ToString() + "\"><img src=\"App_Themes/Image/icon_page_04.gif\" alt=\"最后一页\" width=\"21px\" height=\"20px\" border=\"0\" align=\"absmiddle\" /></a> ";
        }
        strPage = strPage + " 跳转到第 <input id=\"pageNo\" type=\"text\" size=\"2\" /> 页 <img src=\"App_Themes/Image/icon_page_go.gif\" alt=\"点击跳转\" width=\"30px\" height=\"20px\" border=\"0\" align=\"absmiddle\" style=\"cursor:hand\" OnClick=\"PageGo(\'" + intPageCount.ToString() + "\',\'" + strUrl + "\')\" /> ";
        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("<center><strong>提示:没有找到任何数据.</strong></center>");
            this.PanelTable.Visible = false;
        }
    }
}

完整源程序: 点击下载此文件
posted @ 2006-11-03 11:48  老冯  阅读(324)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3