DataList分页(转载)

DataList分页(转载)

前代码

<HTML>
 <HEAD >
  <title>无标题页</title>
 </HEAD>
 <body>
  <form runat="server">
   <div>
    <TABLE style="Z-INDEX: 101; LEFT: 32px; WIDTH: 752px; ; TOP: 16px; HEIGHT: 312px"
     cellSpacing="0" cellPadding="0" width="752" border="0">
     <TR>
      <TD style="HEIGHT: 29px"><FONT face="宋体">DataList分页技术和超级链接</FONT></TD>
     </TR>
     <TR>
      <TD style="HEIGHT: 252px"><asp:datalist runat="server" Height="96px" Width="576px">
        <HeaderTemplate>
         定单编号<TD>
         员工编号<TD>
         定单日期<TD>
         运费<TD>
         运往所在城市
        </HeaderTemplate>
        <ItemTemplate>
         <%# DataBinder.Eval(Container.DataItem,"OrderID")%>
         <TD><%# DataBinder.Eval(Container.DataItem,"CustomerID")%>
         <TD><%# DataBinder.Eval(Container.DataItem,"OrderDate")%>
         <TD><%# DataBinder.Eval(Container.DataItem,"Freight")%>
         <TD><%# DataBinder.Eval(Container.DataItem,"ShipCity")%>
        </ItemTemplate>
       </asp:datalist></TD>
     </TR>
     <TR>
      <TD><FONT face="宋体"><asp:linkbutton runat="server" CommandName="first" OnCommand="LinkButton_Click">第一页</asp:linkbutton><asp:linkbutton runat="server" CommandName="prev" OnCommand="LinkButton_Click">上一页</asp:linkbutton><asp:linkbutton runat="server" CommandName="next" OnCommand="LinkButton_Click">下一页</asp:linkbutton><asp:linkbutton runat="server" CommandName="end" OnCommand="LinkButton_Click">最后一页</asp:linkbutton>总<asp:label runat="server"></asp:label>页
        当前第<asp:label runat="server"></asp:label>页
        <asp:linkbutton runat="server" CommandName="jump" OnCommand="LinkButton_Click">跳到</asp:linkbutton>第
        <asp:textbox runat="server" Width="90px"></asp:textbox>页</FONT></TD>
     </TR>
    </TABLE>
   </div>
  </form>
 </body>
</HTML>

 

 

CS代码

 

int CurrentPage;//当前页数
  int PageSize;   //每页条数
  int PageCount;
  protected System.Web.UI.WebControls.DataList DataList1;
  protected System.Web.UI.WebControls.LinkButton FirstLB;
  protected System.Web.UI.WebControls.LinkButton PreviousLB;
  protected System.Web.UI.WebControls.LinkButton NextLB;
  protected System.Web.UI.WebControls.LinkButton EndLB;
  protected System.Web.UI.WebControls.Label TotalLbl;
  protected System.Web.UI.WebControls.Label CurrentLbl;
  protected System.Web.UI.WebControls.LinkButton JumpLB;
  protected System.Web.UI.WebControls.TextBox TextBox1;  //总页数
  int RecordCount;//总条数
  private void Page_Load(object sender, System.EventArgs e)
  {
   PageSize = 10;//每页10条记录
   if (!Page.IsPostBack)
   {           
    CurrentPage = 0;//当前页习惯设为0
    ViewState["PageIndex"] = 0;//页索引也设为0
    //计算总共有多少记录
    RecordCount = CalculateRecord();


    //计算总共有多少页
    if (RecordCount % PageSize == 0)
    {
     PageCount = RecordCount / PageSize;
    }
    else
    {
     PageCount = RecordCount / PageSize + 1;
    }

    this.TotalLbl.Text = PageCount.ToString();//显示总页数
    ViewState["PageCount"] = PageCount;//会话session 对整个 application 有效 ,而视图状态 viewstate相当于某个页面的 session

    this.DataListBind();//不可以放在初始化条件之前就绑定,那样的话,如果仅有一页的数据,“下一页”页仍然显示
           
   }
  }


  //计算总共有多少条记录
  private int CalculateRecord()
  {
   try
   {
    int recordCount;
    SqlConnection con = new SqlConnection("workstation size=4096;user source=192.168.1.31;persist security info=False;initial catalog=Northwind");//数据库使用Northwind;
    con.Open();

    string sql = "select count(*) as count from orders";
    SqlCommand cmd = new SqlCommand(sql, con);
    SqlDataReader sdr = cmd.ExecuteReader();

    if (sdr.Read())
    {
     recordCount = Int32.Parse(sdr["count"].ToString());               
    }


    else
    {
     recordCount = 0;
    }

    sdr.Close();
    con.Close();
    return recordCount;
   }


   catch (Exception ex)
   {
    throw new Exception(ex.Message);
   }
  }


  //将数据绑定到Datalist控件
  public void DataListBind()
  {
   try
   {
    int StartIndex = CurrentPage * PageSize;//设定导入的起终地址
    string sql = "select * from orders";
    DataSet ds = new DataSet();
    SqlConnection con = new SqlConnection("workstation size=4096;user source=192.168.1.31;persist security info=False;initial catalog=Northwind");
    con.Open();

    SqlDataAdapter sda = new SqlDataAdapter(sql, con);
    sda.Fill(ds, StartIndex, PageSize, "orders");//这是sda.Fill方法的第一次重载,里面的变量分别是数据集DataSet ,开始记录数StartRecord,最大的记录数MaxRecord,数据表名TableName
    this.DataList1.DataSource = ds.Tables["orders"].DefaultView;
    this.DataList1.DataBind();
    this.PreviousLB.Enabled = true;
    this.NextLB.Enabled = true;
    if (CurrentPage == (PageCount - 1)) this.NextLB.Enabled = false;//当为最后一页时,下一页链接按钮不可用
    if (CurrentPage == 0) this.PreviousLB.Enabled = false;//当为第一页时,上一页按钮不可用
    this.CurrentLbl.Text = (CurrentPage + 1).ToString();//当前页数

   }


   catch (Exception ex)
   {
    throw new Exception(ex.Message);
   }
  }

  public void LinkButton_Click(Object sender, CommandEventArgs e)//自己编写的按钮点击事件
  {
   CurrentPage = (int)ViewState["PageIndex"];//获得当前页索引
   PageCount = (int)ViewState["PageCount"];//获得总页数


   string cmd = e.CommandName;

   //判断cmd,以判定翻页方向


   switch (cmd)
   {
    case "prev"://上一页
     if (CurrentPage > 0) CurrentPage--;
     break;

    case "next":
     if (CurrentPage < (PageCount - 1)) CurrentPage++;//下一页
     break;

    case "first"://第一页
     CurrentPage = 0;
     break;

    case "end"://最后一页
     CurrentPage = PageCount - 1;
     break;

    case "jump"://跳转到第几页
     if (this.TextBox1.Text.Trim() == "" || Int32.Parse(this.TextBox1.Text.Trim()) > PageCount)//如果输入数字为空或超出范围则返回
     {
      return;
     }
     else
     {                   
      CurrentPage = Int32.Parse(this.TextBox1.Text.ToString()) - 1;
      break;
     }
   }
   ViewState["PageIndex"] = CurrentPage;//获得当前页

   this.DataListBind();//重新将DataList绑定到数据库
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值