datalist分页


private void Page_Load(object sender, System.EventArgs e)
{
     pagesize = 10;
 //显示总页数
 pagecount = getpgcount();
 lbPageCount.Text ="共 "+pagecount.ToString ()+" 页";
   
 if (!Page.IsPostBack )
 {
   ViewState["pageindex"]=0;
   BindData();
 }
 else
 {
   currentpage=(int)ViewState["pageindex"];
 }
   
}

  

#region  数据绑定
private void BindData()
{
    string strcon="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath ("..//penhui.mdb");
    OleDbConnection con=new OleDbConnection (strcon);
    con.Open ();

    string str_sel="SELECT user_tab.username,guestbook_tab.guestid,
guestbook_tab.zhuti,guestbook_tab.content,guestbook_tab.systime,
guestbook_tab.recontent FROM guestbook_tab ,user_tab where
guestbook_tab.userid = user_tab.userid order by guestbook_tab.systime desc";

    OleDbDataAdapter da=new OleDbDataAdapter (str_sel,con);

    DataSet ds=new DataSet ();
    da.Fill(ds,(currentpage*pagesize),pagesize,"Guest");
   
    mylist.DataSource =ds.Tables ["Guest"].DefaultView ;
    mylist.DataBind ();

    da.Dispose ();
    con.Close ();

}
#endregion


//计算总记录数
protected int rscount()
{
   
    string strcon="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("..//penhui.mdb");
    OleDbConnection conn = new OleDbConnection(strcon);
    conn.Open();

    string selstr = "select count(*) as icount from guestbook_tab ";
    OleDbCommand com = new OleDbCommand(selstr,conn);
    OleDbDataReader dr = com.ExecuteReader();
    if(dr.Read())
    {
        int icount = Int32.Parse(dr["icount"].ToString());
        lbRecordcount.Text ="共 "+icount.ToString ()+" 条记录";
        dr.Close();
        conn.Close();
        return icount;
    }
    else
    {
        return 0;
     }
   
}
  
//总页数
protected int getpgcount()
{
   int jlcount = rscount();
   int pgcount;
   if(jlcount%pagesize==0)
   {
      pgcount = (jlcount/pagesize);
    }
   else
   {
      pgcount = (jlcount/pagesize) + 1;
    }
   return pgcount;
}
  
    
//上页
private void link_pre_Click(object sender, System.EventArgs e)
{
    currentpage=(int)ViewState["pageindex"];
    if(currentpage>0)currentpage--;
       ViewState["pageindex"]=currentpage;
    BindData();
}

//下页
private void link_next_Click(object sender, System.EventArgs e)
{
    currentpage=(int)ViewState["pageindex"];
    if(currentpage<(pagecount-1))currentpage++;
 ViewState["pageindex"]=currentpage;
    BindData();;
  
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值