NET DataList 分页源码 (转)

 private const int PAGECOUNT = 9;
    private static int nPhotoCount = 0;
    private int nPhotoAllPageCount = 0;
    private int nPhotoCurrentPage = 0;

    protected void Page_Load(object sender, EventArgs e)
    {

        if (ViewState["PhotoCurrentPage"] != null)
        {
            nPhotoCurrentPage = Int32.Parse(ViewState["PhotoCurrentPage"].ToString());
        }
        if (!Page.IsPostBack)
        {
            BindPhotoData();
            //重新绑定当前的页码   
            Label3.Text = (nPhotoCurrentPage + 1).ToString() + " ";
        }
    }
    private void BindPhotoData()
    {
        SqlDataAdapter ad = new SqlDataAdapter("select * from sysobjects","server=.;database=master;uid=sa;pwd=;");
        DataSet set = new DataSet();
        ad.Fill(set);
        nPhotoCount = set.Tables[0].Rows.Count;
        nPhotoAllPageCount = nPhotoCount / PAGECOUNT;
        if (nPhotoAllPageCount * PAGECOUNT < nPhotoCount)
        {
            nPhotoAllPageCount++;
        }
        //照片的总数量和总页数
        Label1.Text = nPhotoAllPageCount.ToString() + " ";
        Label2.Text = nPhotoCount.ToString() + " ";
        DataSet ds =GetPhotoes(nPhotoCurrentPage * PAGECOUNT, PAGECOUNT);
        DataList1.DataSource = ds;
        DataList1.DataBind();
    }
    public DataSet GetPhotoes(int a,int b)
    {
        SqlDataAdapter ad = new SqlDataAdapter("select * from sysobjects", "server=.;database=master;uid=sa;pwd=;");
        DataSet set = new DataSet();
        ad.Fill(set, a, b, "sysobjects");
        return set;

    }
     private void Page_Click(object sender, System.EventArgs e)
    {
        String commangArg = ((LinkButton)sender).CommandArgument;
        switch (commangArg)
        {
            case "First":
                {
                    nPhotoCurrentPage = 0;
                    break;
                }
            case "Prev":
                {
                    nPhotoCurrentPage = (int)Math.Max(0, nPhotoCurrentPage - 1);
                    break;
                }
            case "Next":
                {
                    nPhotoCurrentPage = (int)Math.Min(nPhotoCount - 1, nPhotoCurrentPage + 1);
                    break;
                }
            case "Last":
                {
                    nPhotoCurrentPage = nPhotoCount - 1;
                    break;
                }
            default:
                {
                    break;
                }
        }
        ViewState["PhotoCurrentPage"] = nPhotoCurrentPage.ToString();
        BindPhotoData(); 
        Label3.Text = (nPhotoCurrentPage + 1).ToString() + " ";
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值