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();;
}