使用分页控件AspNetPager分页

一、创建AspNetPager对象
<webdiyer:AspNetPager id="page"  runat="server"
PrevPageText="上页" NextPageText="下页" LastPageText="尾页" FirstPageText="首页" SubmitButtonText="转到" ShowInputBox="Always"
PageSize="2" CustomInfoSectionWidth="50%" ShowCustomInfoSection="Left">
</webdiyer:AspNetPager>

<asp:Label Runat=server ID=lblMsg  Visible=False></asp:Label>
二、添加后台代码
1.导入名称空间,using System.Data.SqlClient;或using System.Data.OleDbClient;定义一个变量flag,标识是否有符合条件的记录。
2.写方法:第一次加载数据,重新加载数据。
protected int flag;
private void Page_Load(object sender, System.EventArgs e)
  {
   if(!this.IsPostBack)
   {
    SqlConnection conn=new SqlConnection(DAL.SQLHelper.CONN_STRING);
    conn.Open();
    SqlCommand com=new SqlCommand("select count(*) from tbl_serverice",conn);
    page.RecordCount=Convert.ToInt32(com.ExecuteScalar());
   
    flag=page.RecordCount;
 
    Bind();
    if(flag==0)
   {
    this.lblMsg.Text="没有找到符合条件的相关记录";
    this.lblMsg.Visible=true;
   }
   else
   {
     this.lblMsg.Visible=false;
   }
   }

   
   
  }

 


void Bind()
  {
   SqlConnection conn=new SqlConnection(DAL.SQLHelper.CONN_STRING);
   conn.Open();
   SqlDataAdapter ada=new SqlDataAdapter("select * from tbl_serverice where serv_id=1212",conn);
   DataSet ds=new DataSet();
   

   //先加载部分数据
   // ACda.Fill(ACds,haha.PageSize*(haha.CurrentPageIndex-1),haha.PageSize,"CurDataTable");

   ada.Fill(ds,page.PageSize*(page.CurrentPageIndex-1),page.PageSize,"curdata");
   ada.Fill(ds,"alldata");

   //this.dg.VirtualItemCount=ds.Tables["alldata"].Rows.Count;//DataGrid须用此句程式
   this.flag=ds.Tables["alldata"].Rows.Count;

   this.dlServAdv.DataSource=ds.Tables["curdata"];
   this.dlServAdv.DataBind();

   page.CustomInfoText=" 共有记录<font color=\"blue\"><b>"+page.RecordCount.ToString()+"</b></font>条";
   page.CustomInfoText+=" 共有<font color=\"blue\"><b>"+page.PageCount.ToString()+"</b></font>页";
   page.CustomInfoText+=" 当前为第<font color=\"red\"><b>"+page.CurrentPageIndex.ToString()+"</b></font>页";
   conn.Dispose();
   ada.Dispose();
  }

 

private void AspNetPager1_PageChanged(object src, Wuqi.Webdiyer.PageChangedEventArgs e)
  {
   page.CurrentPageIndex=e.NewPageIndex;
   this.Bind();
  }


 

posted @ 2007-09-06 01:53  yellowwood  阅读(319)  评论(0编辑  收藏  举报
Never Give UP