datalist的分页

 

using System;
using System.Data;
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.Data.SqlClient;

public partial class houtai_tizi_YHtiezi_chakan : System.Web.UI.Page
{
    int RecoudCount;//总记录数
    int PageCount;//总页数
    int PageSize = 3;//每一页显示记录
    int CurrentPage = 0;//单前现实的页数
    protected void Page_Load(object sender, EventArgs e)
    {
       
         
#region  获取总记录数
        string countId = Request.QueryString["countid"];
        sqlstr = "select count(*)as count from tb_hf where contId=" + countId;
        ds = DB.GetDataTableBySql(sqlstr);
        if (ds.Tables[0].Rows.Count != 0)
        {
            RecoudCount = Convert.ToInt32(ds.Tables[0].Rows[0]["count"].ToString());
        }
        else
        {
            RecoudCount = 0;
        }
        //获取总页数
        if (RecoudCount % PageSize == 0)
        {
            PageCount = RecoudCount / PageSize;
        }
        else
        {
            PageCount =( RecoudCount / PageSize)+1;
        }
        ViewState["PageCount"] = PageCount;
        this.Label1.Text = PageCount.ToString();
    }

//主要代码
    public void HF_Bind()
    {
        int StartIndex = PageSize * CurrentPage;
        string countId = Request.QueryString["countid"];
        sqlstr = "select * from user_hf where ContId=" + countId ;
        //ds = DB.GetDataTableBySql(sqlstr);
        SqlConnection con = new SqlConnection();
        con.ConnectionString = DB.GetConnectString();
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter(sqlstr,con);
        da.Fill(ds, StartIndex, PageSize, "回复表");
      
            this.DataList2.DataSource = ds.Tables["回复表"].DefaultView;
            this.DataList2.DataBind();

            PageCount =(int)ViewState["PageCount"] ;

            Button3.Enabled = true;
            Button1.Enabled = true;
            Button4.Enabled = true;
            Button2.Enabled = true;
            if (CurrentPage == 0)
            {
                Button3.Enabled = false;
                Button1.Enabled = false;
            }
             if(CurrentPage==(PageCount-1))
            {
                Button4.Enabled = false;
                Button2.Enabled = false;
            }
            this.Label2.Text = (CurrentPage+1).ToString();
        }
    //第一页
    protected void Button1_Click(object sender, EventArgs e)
    {
        ds.Clear();
        CurrentPage = 0;
        ViewState["CurrentPage"] = CurrentPage;//用状态来保存,当前页,应为每次单击后页面都会刷新,没有保存就没有下一页的数据,下同

        HF_Bind();
    }
    //最后一页
    protected void Button2_Click(object sender, EventArgs e)
    {
        ds.Clear();
        CurrentPage=PageCount-1;
        ViewState["CurrentPage"] = CurrentPage;

        HF_Bind(); 
    }
    //上一页
    protected void Button3_Click(object sender, EventArgs e)
    {
        ds.Clear();
        CurrentPage = (int)ViewState["CurrentPage"];
        CurrentPage = CurrentPage-1;
        ViewState["CurrentPage"] = CurrentPage;
        HF_Bind();
    }
    //下一页
    protected void Button4_Click(object sender, EventArgs e)
    {
        ds.Clear();
        CurrentPage = (int)ViewState["CurrentPage"];
        CurrentPage = CurrentPage+1;
        ViewState["CurrentPage"] = CurrentPage;
        HF_Bind();
    }
 }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值