当前位置:编程学习 > C#/ASP.NET >>

分页的实现

DataList中如何实现分页功能 --------------------编程问答-------------------- http://topic.csdn.net/u/20101115/15/40c9be9b-a04a-49d8-a65e-a1d8e3043325.html 存储过程+ aspnetPage --------------------编程问答--------------------
private   void   Page_Load(object   sender,   System.EventArgs   e)   
  {   
  //   在此处放置用户代码以初始化页面   
            PageSize   =   10;   
            sql="select   *   from   products   order   by   all_time   desc   ";   
            if(!Page.IsPostBack)   
            {   
            //计算总共有多少记录       
              RecordCount   =   CalculateRecord();     
            //计算总共有多少页   
          //取整       
              PageCount   =   RecordCount/PageSize;       
              if   (RecordCount%PageSize   >   0)       
              PageCount   =   PageCount   +   1;       
              lblPageCount.Text   =   PageCount.ToString();       
              lblRecordCount.Text   =   RecordCount.ToString();       
              ViewState["PageCount"]   =   PageCount;       
              CurrentPage   =   0;       
              ViewState["PageIndex"]   =   0;       
              //绑定       
              ListBind();   
    
              }   
  }   
  public   int   CalculateRecord()       
  {       
  int   intCount;       
  string   strCount   =   "select   count(*)   as   co   from   products";     
  SqlConnection   Con=new   SqlConnection(data.constr);   
  SqlCommand   addCommand=new   SqlCommand(strCount,Con);   
  addCommand.Connection.Open();   
  SqlDataReader   dr;   
  dr=addCommand.ExecuteReader();   
  if(dr.Read())       
  {       
        intCount   =   Int32.Parse(dr["co"].ToString());       
  }       
  else       
  {       
        intCount   =   0;       
  }       
        dr.Close();       
        return   intCount;       
  }       
    
  ICollection   CreateSource()       
  {       
    
  int   StartIndex;       
  //设定导入的起终地址       
  StartIndex   =   CurrentPage*PageSize;       
  string   strSel   =   "select   *   from   products";     
  SqlConnection   Con=new   SqlConnection(data.constr);   
  DataSet   ds   =   new   DataSet();       
  SqlDataAdapter   MyAdapter   =   new   SqlDataAdapter(strSel,Con);       
  MyAdapter.Fill(ds,StartIndex,PageSize,"products");       
  return   ds.Tables["products"].DefaultView;       
  }       
  public   void   ListBind()       
  {       
  dl1.DataSource   =   CreateSource();       
  dl1.DataBind();       
  lbnNextPage.Enabled   =   true;       
  lbnPrevPage.Enabled   =   true;       
  if(PageCount==0)       
  {       
  lblCurrentPage.Text   =   "0";       
  lbnNextPage.Enabled   =   false;       
  lbnPrevPage.Enabled   =   false;       
  }       
  else       
  {       
  if(CurrentPage==(PageCount-1))   lbnNextPage.Enabled   =   false;       
  if(CurrentPage==0)   lbnPrevPage.Enabled   =   false;       
  lblCurrentPage.Text   =   (CurrentPage+1).ToString();       
  }       
  }       
  public   void   Page_OnClick(Object   sender,CommandEventArgs   e)       
  {       
  CurrentPage   =   (int)ViewState["PageIndex"];       
  PageCount   =   (int)ViewState["PageCount"];       
  string   cmd   =   e.CommandName;       
  //判断cmd,以判定翻页方向       
  switch(cmd)       
  {       
  case   "next":       
  if(CurrentPage<(PageCount-1))   CurrentPage++;       
  break;       
  case   "prev":       
  if(CurrentPage>0)   CurrentPage--;       
  break;   
  case   "first":   
  CurrentPage=0;   
  break;   
  case   "last":   
                                      CurrentPage=PageCount-1;   
  break;   
  }       
    
  ViewState["PageIndex"]   =   CurrentPage;       
    
  ListBind();       
    
  }       
  HTML   部分:   
  <TABLE   id="Table2"   style="Z-INDEX:   102;   LEFT:   128px;   WIDTH:   648px;   POSITION:   absolute;   TOP:   432px;   HEIGHT:   27px"cellSpacing="1"   cellPadding="1"   width="648"   border="1">   
  <TR>   
  <TD><FONT   face="宋体">共<asp:label   id="lblRecordCount"   runat="server">Label</asp:label>条记录    共<asp:label   id="lblPageCount"   runat="server">Label</asp:label>页   当前第<asp:label   id="lblCurrentPage"   runat="server">Label</asp:label>页 <asp:LinkButton   id="lbnFirstPage"   runat="server"   OnCommand="Page_OnClick"   CommandName="first">首页</asp:LinkButton> <asp:linkbuttonid="lbnPrevPage"runat="server"OnCommand="Page_OnClick"   CommandName="prev">上一页</asp:linkbutton>  <asp:linkbutton   id="lbnNextPage"   runat="server"   OnCommand="Page_OnClick"   CommandName="next">下一页</asp:linkbutton>    
  <asp:LinkButton   id="lbnLastPage"   runat="server"   OnCommand="Page_OnClick"   CommandName="last">尾页</asp:LinkButton></FONT></TD></TR></TABLE>   
--------------------编程问答-------------------- 没用过,我都是自己写分页函数的。 --------------------编程问答-------------------- DataList实现分页和排序
--------------------编程问答-------------------- --------------------编程问答-------------------- 谢谢各位了 --------------------编程问答-------------------- 结贴咯 --------------------编程问答-------------------- google  一个分页存储过程  
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,