DataList的分页

html页面代码:

<asp:DataList ID="DataList" runat="server" RepeatDirection="Horizontal" RepeatColumns="6">        <ItemTemplate>    

 <table width="130" border="0" align="left" cellpadding="4" cellspacing="5" class="line3 pic1">        <tbody>         

 <tr>          

  <td align="middle"><a   href='Display.aspx?ID=<%#DataBinder.Eval(Container.DataItem,"GroupID") %>'    target="_blank"><img src='../<%#DataBinder.Eval(Container.DataItem,"ImageUrl") %>'                             width="130" height="90" border="0" /></a></td>      

    </tr>         

 <tr>            <td height="26"><div align="center"><a class="tt2"    href="Diaplay.aspx?ID=<%#DataBinder.Eval(Container.DataItem,"GroupID") %>"    target="_blank"><%#DataBinder.Eval(Container.DataItem,"FileName") %></a></div></td>        

  </tr>       

 </tbody>   

   </table>   

 </ItemTemplate>   

   </asp:DataList> 

  <div class="pagination" style="width: 754px"> 

<span style="font-size: 10pt">  

 共有<asp:Label ID="lblRecordCount" runat="server" ForeColor="red"></asp:Label>记录  

当前为<asp:Label ID="lblCurrentPage" runat="server" ForeColor="Red"></asp:Label>/   <asp:Label ID="lblPageCount" runat="server" ForeColor="Red"></asp:Label>页   

<asp:DropDownList ID="Ddl_PageNumber" runat="server" AutoPostBack="true"></asp:DropDownList>   <asp:linkbutton   id="BtnFirst"   runat="server"   Text="首页"   OnCommand="Page_OnClick"    CommandName="First" Font-Size="10pt">首页</asp:linkbutton>                  

      <asp:LinkButton ID="lbnPrevPage" runat="server" CommandName="prev" OnCommand="Page_OnClick"                            Text="上一页" Font-Size="10pt"></asp:LinkButton>&nbsp;                   

<asp:LinkButton ID="lbnNextPage" runat="server" CommandName="next" OnCommand="Page_OnClick"                            Text="下一页" Font-Size="10pt" Width="39px"></asp:LinkButton>                           

<asp:linkbutton   id="BtnLast"   OnCommand="Page_OnClick" CommandName="Last"     text="尾页"   Runat="server" Font-Size="10pt">尾页</asp:linkbutton></span>

cs代码:

  private void Group()   

  {       

PageSize = 24;          //设定PageSize      

   RecordCount = CalculateRecord(); //计算总共有多少记录/     

   PageCount = RecordCount / PageSize; //计算总共有多少页       

  if(RecordCount % PageSize > 0)     //取整          

   PageCount = PageCount + 1;         

   this.lblPageCount.Text = PageCount.ToString();      

      lblRecordCount.Text = RecordCount.ToString();       

     ViewState["PageCount"] = PageCount;        

    CurrentPage = 0;          

  ViewState["PageIndex"] = 0;           

  Al_PageNum = new ArrayList();//绑定DROPDOWNLIST        

    for (int i = 1; i <= PageCount; i++)   //从1开始循环,为了不出现0页码           

     Al_PageNum.Add(i.ToString());           

Ddl_PageNumber.DataSource = Al_PageNum;           

Ddl_PageNumber.DataBind();         

   ListBind();                         //绑定   

  }    

public void ListBind()   

  {       

 DataList.DataSource = CreateSource();       

 DataList.DataBind();       

lbnNextPage.Enabled = true;     

   lbnPrevPage.Enabled = true;       

 BtnFirst.Enabled = true;       

 BtnLast.Enabled = true;       

 if (PageCount == 0)     

   {          

  lblCurrentPage.Text = "0";        

    lbnNextPage.Enabled = false;   

         lbnPrevPage.Enabled = false;      

      BtnFirst.Enabled = false;        

    BtnLast.Enabled = false;     

   }       

else      

  {          

  if (CurrentPage == (PageCount - 1))          

      lbnNextPage.Enabled = false;          

  if (CurrentPage == 0)           

     lbnPrevPage.Enabled = false;        

    lblCurrentPage.Text = (CurrentPage + 1).ToString();       

 }      

  Ddl_PageNumber.Text = lblCurrentPage.Text;    

}    

 ICollection CreateSource()   

{      

  Myconn = DB.CreateDB();       

 Myconn.Open();       

int StartIndex;                               //设定导入的起终地址        

 StartIndex = CurrentPage * PageSize;            //计算记录数的起始点      

  string strSel = "select * from groupFile where Filetype='嘉业团队' order by GroupID desc";       

 OleDbCommand cmd = new OleDbCommand(strSel,Myconn);       

 DataSet ds = new DataSet();       

OleDbDataAdapter dap = new OleDbDataAdapter(cmd);       

dap.Fill(ds, StartIndex, PageSize, "groupFile");       

 return ds.Tables["groupFile"].DefaultView;   

 }    

 /// <summary>    /// 计算总共有多少条记录  

  /// </summary>    /

// <returns></returns>  

  private int CalculateRecord()   

  {       

Myconn = DB.CreateDB();       

 Myconn.Open();      

  int intCount;       

 string strCount = "select count(*) as co from groupFile where Filetype='嘉业团队'";       

 OleDbCommand com = new OleDbCommand(strCount, Myconn);     

   OleDbDataReader dr = com.ExecuteReader();    

    if (dr.Read())     

   {           

 intCount = Int32.Parse(dr["co"].ToString());    

    }       

else     

   {           

 intCount = 0;     

   }       

dr.Close();       

 Myconn.Close();       

 return intCount;   

 }    

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 "Last":         

       CurrentPage = (PageCount - 1);          

      break;          

  default:           

     CurrentPage = 0;           

     break;     

   }        

ViewState["PageIndex"] = CurrentPage;       

ListBind();   

 }    

public void PageNum_SelectIndexChanged(object sender, System.EventArgs e)   

 {       

 ViewState["PageIndex"] = int.Parse(Ddl_PageNumber.SelectedItem.Value) - 1;//保持不出现0页码      

  PageSize = 4;      

  CurrentPage = (int)ViewState["PageIndex"];       

 PageCount = (int)ViewState["PageCount"];      

  ListBind();   

 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值