DataList分页

HTML:
    <table align="center" id="table1" width="80%">
          <tr>
           <td>
            <asp:linkbutton id="fp1" runat="server" CommandName="first">首页</asp:linkbutton>
            <asp:linkbutton id="pp1" runat="server" CommandName="prev">上一页</asp:linkbutton>
            <asp:linkbutton id="np1" runat="server" CommandName="next">下一页</asp:linkbutton>
            <asp:linkbutton id="lp1" runat="server" CommandName="last">尾页</asp:linkbutton>
            <asp:label id="Label1" runat="server">页次</asp:label>
            <asp:Label id="cp1" runat="server"></asp:Label>
            <asp:label id="Label2" runat="server">/</asp:label>
            <asp:Label id="pc1" runat="server"></asp:Label>
            <asp:label id="Label3" runat="server">每页记录数</asp:label>
            <asp:Label id="rc1" runat="server"></asp:Label>
            <asp:Label id="Label4" runat="server">选择跳转页码</asp:Label>
            <asp:DropDownList id="jp1" runat="server" AutoPostBack="True"></asp:DropDownList>
           </td>
          </tr>
         </table>
CS:
    

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
namespace yalong
{
 /// <summary>
 /// gc_anli 的摘要说明。
 /// </summary>
 public class gc_anli : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.DataList dlCase;
  protected System.Web.UI.WebControls.LinkButton fp1;
  protected System.Web.UI.WebControls.LinkButton pp1;
  protected System.Web.UI.WebControls.LinkButton np1;
  protected System.Web.UI.WebControls.LinkButton lp1;
  protected System.Web.UI.WebControls.Label Label1;
  protected System.Web.UI.WebControls.Label cp1;
  protected System.Web.UI.WebControls.Label Label2;
  protected System.Web.UI.WebControls.Label pc1;
  protected System.Web.UI.WebControls.Label Label3;
  protected System.Web.UI.WebControls.Label rc1;
  protected System.Web.UI.WebControls.Label Label4;
  protected System.Web.UI.WebControls.DropDownList jp1;

  protected int PageSize,RecordCount,PageCount,CurrentPage;  
  OleDbConnection conn;
  DAL op=new DAL();
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   PageSize =6;
   try
   {
    string connstr =op.strConn;
    conn = new OleDbConnection(connstr);
    rc1.Text=PageSize.ToString();
    CurrentPage=0;
    RecordCount=this.GetRecordCount();
    PageCount = (RecordCount%PageSize)>0?(RecordCount/PageSize)+1:(RecordCount/PageSize);
    pc1.Text=PageCount.ToString();
    ArrayList al=new ArrayList();
    for(int i=1;i<PageCount+1;i++)
    {
     al.Add(i);
    }
    if(!Page.IsPostBack)
    {
     jp1.DataSource=al;
     jp1.DataBind();
     mfbind();
    }
   }
   catch
   {
    Response.Redirect("Error.htm");
   }
  }

  public int GetRecordCount()
  {
   
   
   conn.Open();
   OleDbCommand cmd=new OleDbCommand("select count(*) as cnt from ccase",conn);
   int intCount=int.Parse(cmd.ExecuteScalar().ToString());
   conn.Close();
   cmd.Dispose();
   return intCount;
  
  }
  private IList DataSource()
  {

   
   int StartIndex;
   StartIndex = CurrentPage*PageSize;
   string strSql = "select * from ccase";
   DataSet ds = new DataSet();
   OleDbDataAdapter da = new OleDbDataAdapter(strSql,conn);
   da.Fill(ds,StartIndex,PageSize,"ccase");
   return ds.Tables["ccase"].DefaultView;
   
  
  }
  private void mfbind()
  {
   try
   {
    this.dlCase.DataSource=(DataView)DataSource();
    this.dlCase.DataBind();
    np1.Enabled = true;
    pp1.Enabled = true;
    if(CurrentPage==(PageCount-1)) np1.Enabled = false;
    if(CurrentPage==0) pp1.Enabled = false;
    cp1.Text = (CurrentPage+1).ToString();
   }
   catch
   {
    Response.Redirect("Error.htm");
   }
   
  }

 
  public void Page_OnClick(Object sender,CommandEventArgs e)
  {
   
   CurrentPage =int.Parse(cp1.Text.Trim())-1;
   PageCount =int.Parse(pc1.Text.Trim());

   string cnstr = e.CommandName;
   switch(cnstr)
   {
    case "first":
     CurrentPage=0;
     break;
    case "next":
     if(CurrentPage<(PageCount-1)) CurrentPage++;
     break;
    case "prev":
     if(CurrentPage>0) CurrentPage--;
     break;
    case "last":
     CurrentPage=PageCount-1;
     break;
   }
   mfbind();
   
   
  }

  private void jp1_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   try
   {
    CurrentPage=int.Parse(jp1.SelectedItem.Text.Trim())-1;
    mfbind();
   }
   catch
   {
    Response.Redirect("Error.htm");
   }
  }


  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.fp1.Command += new System.Web.UI.WebControls.CommandEventHandler(this.Page_OnClick);
   this.pp1.Command += new System.Web.UI.WebControls.CommandEventHandler(this.Page_OnClick);
   this.np1.Command += new System.Web.UI.WebControls.CommandEventHandler(this.Page_OnClick);
   this.lp1.Command += new System.Web.UI.WebControls.CommandEventHandler(this.Page_OnClick);
   this.jp1.SelectedIndexChanged += new System.EventHandler(this.jp1_SelectedIndexChanged);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion
 }
}

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