全局变量:
//每页显示几行
const int p_count=10;
表格的绑定方法:
/// <summary>
/// msg表格的数据绑定
/// </summary>
/// <param name="sql">查询的SQL语句</param>
/// <param name="startNum">起始行</param>
/// <param name="maxNum">共显示的行数</param>
/// <returns>返回用参数sqlmsg查询出的表中记录的总数</returns>
int DataGrid1_display(string sql,int startNum,int maxNum)
{
DataSet dataset=db.returnDS(sql,startNum,maxNum);
DataTable table=dataset.Tables[0];
//绑定表格
this.DataGrid1.DataSource=table;
this.DataGrid1.DataBind();
dataset=db.returnDS(sql);
//如果小于等于要显示的行数则翻页区不显示
int sum=dataset.Tables[0].Rows.Count;
if(sum<=p_count)
{
this.Table2.Visible=false;
return sum;
}
this.Table2.Visible=true;
//如果起始行为0,设置页码和总页码
if(startNum==0)
{
this.labNum.Text="1";
//得到一共应该显示的页数
if(sum%p_count>0)
{
sum=sum/p_count+1;
}
else
{
sum=sum/p_count;
}
this.labSum.Text=sum.ToString();
this.link_BOF();
}
return sum;
}
//如果是记录首
void link_BOF()
{
this.link1.Enabled=false;
this.link2.Enabled=false;
this.link3.Enabled=true;
this.link4.Enabled=true;
}
//如果是记录尾
void link_EOF()
{
this.link1.Enabled=true;
this.link2.Enabled=true;
this.link3.Enabled=false;
this.link4.Enabled=false;
}
void link_middle()
{
this.link1.Enabled=true;
this.link2.Enabled=true;
this.link3.Enabled=true;
this.link4.Enabled=true;
}
//第一页
private void link1_Click(object sender, System.EventArgs e)
{
switch(this.type)
{
case "msg":
this.DataGrid1_display(sqlmsg,0,p_count);
break;
case "mail":
this.DataGrid2_display(sqlmail,0,p_count);
break;
case "note":
break;
}
}
//上一页
private void link2_Click(object sender, System.EventArgs e)
{
int sum=int.Parse(this.labSum.Text);
int num=int.Parse(this.labNum.Text);
switch(this.type)
{
case "msg":
this.DataGrid1_display(sqlmsg,num*p_count-2*p_count,p_count);
break;
case "mail":
this.DataGrid2_display(sqlmail,num*p_count-2*p_count,p_count);
break;
case "note":
break;
}
this.labNum.Text =(num-1).ToString();
if((num-1)==1)
{
this.link_BOF();
}
else
{
this.link_middle();
}
}
//下一页
private void link3_Click(object sender, System.EventArgs e)
{
int sum=int.Parse(this.labSum.Text);
int num=int.Parse(this.labNum.Text);
switch(this.type)
{
case "msg":
this.DataGrid1_display(sqlmsg,num*p_count,p_count);
break;
case "mail":
this.DataGrid2_display(sqlmail,num*p_count,p_count);
break;
case "note":
break;
}
this.labNum.Text =(num+1).ToString();
if((num+1)==sum)
{
this.link_EOF();
}
else
{
this.link_middle();
}
}
//最后一页
private void link4_Click(object sender, System.EventArgs e)
{
int sum=int.Parse(this.labSum.Text);
switch(this.type)
{
case "msg":
this.DataGrid1_display(sqlmsg,(sum-1)*p_count,p_count);
break;
case "mail":
this.DataGrid2_display(sqlmail,(sum-1)*p_count,p_count);
break;
case "note":
break;
}
this.labNum.Text =sum.ToString();
this.link_EOF();
}
//转页操作
private void IBtnGo_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
int sum=int.Parse(this.labSum.Text);
int num=int.Parse(this.labNum.Text);
if(int.Parse(this.txtGo.Text)>sum)
{
this.txtGo.Text=this.labNum.Text;
}
if(int.Parse(this.txtGo.Text)<1)
{
this.txtGo.Text=this.labNum.Text;
}
int page=int.Parse(this.txtGo.Text);
switch(this.type)
{
case "msg":
this.DataGrid1_display(sqlmsg,(page-1)*p_count,p_count);
break;
case "mail":
this.DataGrid2_display(sqlmail,(page-1)*p_count,p_count);
break;
case "note":
break;
}
this.labNum.Text =page.ToString();
if(page==sum)
{
this.link_EOF();
}
else if(page==1)
{
this.link_BOF();
}
else
{
this.link_middle();
}
}
//每页显示几行
const int p_count=10;
表格的绑定方法:
/// <summary>
/// msg表格的数据绑定
/// </summary>
/// <param name="sql">查询的SQL语句</param>
/// <param name="startNum">起始行</param>
/// <param name="maxNum">共显示的行数</param>
/// <returns>返回用参数sqlmsg查询出的表中记录的总数</returns>
int DataGrid1_display(string sql,int startNum,int maxNum)
{
DataSet dataset=db.returnDS(sql,startNum,maxNum);
DataTable table=dataset.Tables[0];
//绑定表格
this.DataGrid1.DataSource=table;
this.DataGrid1.DataBind();
dataset=db.returnDS(sql);
//如果小于等于要显示的行数则翻页区不显示
int sum=dataset.Tables[0].Rows.Count;
if(sum<=p_count)
{
this.Table2.Visible=false;
return sum;
}
this.Table2.Visible=true;
//如果起始行为0,设置页码和总页码
if(startNum==0)
{
this.labNum.Text="1";
//得到一共应该显示的页数
if(sum%p_count>0)
{
sum=sum/p_count+1;
}
else
{
sum=sum/p_count;
}
this.labSum.Text=sum.ToString();
this.link_BOF();
}
return sum;
}
//如果是记录首
void link_BOF()
{
this.link1.Enabled=false;
this.link2.Enabled=false;
this.link3.Enabled=true;
this.link4.Enabled=true;
}
//如果是记录尾
void link_EOF()
{
this.link1.Enabled=true;
this.link2.Enabled=true;
this.link3.Enabled=false;
this.link4.Enabled=false;
}
void link_middle()
{
this.link1.Enabled=true;
this.link2.Enabled=true;
this.link3.Enabled=true;
this.link4.Enabled=true;
}
//第一页
private void link1_Click(object sender, System.EventArgs e)
{
switch(this.type)
{
case "msg":
this.DataGrid1_display(sqlmsg,0,p_count);
break;
case "mail":
this.DataGrid2_display(sqlmail,0,p_count);
break;
case "note":
break;
}
}
//上一页
private void link2_Click(object sender, System.EventArgs e)
{
int sum=int.Parse(this.labSum.Text);
int num=int.Parse(this.labNum.Text);
switch(this.type)
{
case "msg":
this.DataGrid1_display(sqlmsg,num*p_count-2*p_count,p_count);
break;
case "mail":
this.DataGrid2_display(sqlmail,num*p_count-2*p_count,p_count);
break;
case "note":
break;
}
this.labNum.Text =(num-1).ToString();
if((num-1)==1)
{
this.link_BOF();
}
else
{
this.link_middle();
}
}
//下一页
private void link3_Click(object sender, System.EventArgs e)
{
int sum=int.Parse(this.labSum.Text);
int num=int.Parse(this.labNum.Text);
switch(this.type)
{
case "msg":
this.DataGrid1_display(sqlmsg,num*p_count,p_count);
break;
case "mail":
this.DataGrid2_display(sqlmail,num*p_count,p_count);
break;
case "note":
break;
}
this.labNum.Text =(num+1).ToString();
if((num+1)==sum)
{
this.link_EOF();
}
else
{
this.link_middle();
}
}
//最后一页
private void link4_Click(object sender, System.EventArgs e)
{
int sum=int.Parse(this.labSum.Text);
switch(this.type)
{
case "msg":
this.DataGrid1_display(sqlmsg,(sum-1)*p_count,p_count);
break;
case "mail":
this.DataGrid2_display(sqlmail,(sum-1)*p_count,p_count);
break;
case "note":
break;
}
this.labNum.Text =sum.ToString();
this.link_EOF();
}
//转页操作
private void IBtnGo_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
int sum=int.Parse(this.labSum.Text);
int num=int.Parse(this.labNum.Text);
if(int.Parse(this.txtGo.Text)>sum)
{
this.txtGo.Text=this.labNum.Text;
}
if(int.Parse(this.txtGo.Text)<1)
{
this.txtGo.Text=this.labNum.Text;
}
int page=int.Parse(this.txtGo.Text);
switch(this.type)
{
case "msg":
this.DataGrid1_display(sqlmsg,(page-1)*p_count,p_count);
break;
case "mail":
this.DataGrid2_display(sqlmail,(page-1)*p_count,p_count);
break;
case "note":
break;
}
this.labNum.Text =page.ToString();
if(page==sum)
{
this.link_EOF();
}
else if(page==1)
{
this.link_BOF();
}
else
{
this.link_middle();
}
}