DataList实现分页功能

新建Default.aspx
怎家DataList控件为其配置数据好数据源
增加四个LinkButton 控件
分别为:
     <asp:LinkButton ID="linkbtnFirst" runat="server" onclick="labFirst_Click">首页</asp:LinkButton>
    
    <asp:LinkButton ID="linkbtnFont" runat="server" onclick="labFont_Click">上一页</asp:LinkButton>
 
    <asp:LinkButton ID="linkbtnNext" runat="server" onclick="labNext_Click">下一页</asp:LinkButton>
    
    <asp:LinkButton ID="linkbtnLast" runat="server" onclick="labLast_Click">最后一页</asp:LinkButton>
在加两个了Label控件
分别为:
    当前页数:[<asp:Label ID="labpage" runat="server" Text="1"></asp:Label>]
       总页数:[<asp:Label ID="labBackPage" runat="server" Text=""></asp:Label>]

在Default.aspx.cs文件中
自定义Mind()方法
  //------------------------------------------------------------
    protected void Page_Load(object sender EnventArgs e)
   {   
       if (!IsPostBack)
        {
            Mind();

        }

   }


  Private void Mind()
 { 
       //获得当前页数
       int curpage= Convert.toInt32(this.labPage.Text);
      // 生成PageDataSource实例
      PageDataSource ps= new PageDataSource();
      SqlConnection con= new SqlConnection("连接字符串");
     SqlDataAdapter sda= new SqlDataAdapter(("select *from BookInfo",con);
     DataSet ds = new DataSet();
     sda.Fill(da,"BooKInfo");
     //获取数据源
     ps.DataSource=da.DataTables["BookInfo"].DefaultView;
     ps.AllowPaging= true;
     ps.PageSize= 3;
     ps.CurrentPageIndex=curpage-1;
      this.linkbtnFirst.Enabled=true;
      this.linkbtnFont.Enabled=true;
      this.linkbtnNext.Enabled=true;
      this.linkbtnLast.Enabled=true;
   if(curpage==1)
    {  
            this.linkbtnFirst.Enabled=false;
            this.linkbtnFont.Enabled= false;
     }
    if(crupage==ps.PageCount)
      {
          this.linkbtnLast.Enabled= false;
          this.linkbtnNext.Enabled= false;
      }

     this.labBackPage=Convert.ToInt32(ps.PageCount);
     this.DataList1.DataSource= ps;
     this.DataList1.DatakeyField="ID";
     DataList1.DataSource=ps;
      con.Close();
}



 protected void linkbtnFirst_Click(object sender, EventArgs e)
    {
        labpage.Text = "1";
        Mind();


    }
    protected void linkbtnFont_Click(object sender, EventArgs e)
    {
        labpage.Text = Convert.ToString (Convert.ToInt32(labpage.Text) - 1);
        Mind();


    }
      protected void linkbtnNext_Click(object sender, EventArgs e)
    {
        labpage.Text = Convert.ToString(Convert.ToInt32(labpage.Text) + 1);
        Mind();


    }
    protected void linkbtnLast_Click(object sender, EventArgs e)
    {
        labpage.Text = labBackPage.Text;
        Mind();

    }



  


          

     



 






posted @ 2008-05-10 22:56  lance2008  阅读(404)  评论(0编辑  收藏  举报