网上找的一个使用aspnetpager控件的使用方法

前台页面代码:Page.aspx
 

Code:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="page.aspx.cs" Inherits="page" %>   
  2.   
  3. <%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>   
  4.   
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
  6.   
  7. <html xmlns="http://www.w3.org/1999/xhtml" >   
  8. <head runat="server">   
  9.     <title>Datalist分页</title>   
  10. </head>   
  11. <body>   
  12.     <form id="form1" runat="server">   
  13.     <div>   
  14.     <asp:DataList ID="list" runat="server">   
  15.      <ItemTemplate>   
  16.      <%# DataBinder.Eval(Container.DataItem,"About_Title") %>   
  17.      </ItemTemplate>   
  18.     </asp:DataList>   
  19.    <webdiyer:AspNetPager ID="pagelist" runat="server" OnPageChanging="PageChanging" FirstPageText="首页" LastPageText="尾页" NextPageText="下一页" PrevPageText="上一页" CustomInfoHTML="第%CurrentPageIndex%页,共%PageCount%页,每页%PageSize%条" ShowFirstLast="False" PageIndexBoxType="TextBox" ShowPageIndexBox="Never"> </webdiyer:AspNetPager>   
  20.     </div>   
  21.     </form>   
  22. </body>   
  23. </html>  

注:不要忘记红色部分

后台页面:
 

Code:
  1. using System;   
  2. using System.Data;   
  3. using System.Configuration;   
  4. using System.Collections;   
  5. using System.Web;   
  6. using System.Web.Security;   
  7. using System.Web.UI;   
  8. using System.Web.UI.WebControls;   
  9. using System.Web.UI.WebControls.WebParts;   
  10. using System.Web.UI.HtmlControls;   
  11. using System.Data.SqlClient;   
  12. using Wuqi.Webdiyer;//别忘记命名空间   
  13.   
  14. public partial class page : System.Web.UI.Page   
  15. {   
  16.     protected void Page_Load(object sender, EventArgs e)   
  17.     {   
  18.         if (!Page.IsPostBack)   
  19.         {   
  20.                
  21.             SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["SQLCONNECTION"].ToString());   
  22.             con.Open();   
  23.             SqlCommand cmd = new SqlCommand();   
  24.             cmd.Connection = con;   
  25.             cmd.CommandText = "SELECT COUNT(*) FROM About";   
  26.             pagelist.AlwaysShow = true;   
  27.             pagelist.PageSize = 10; //显示页数   
  28.             pagelist.RecordCount = (int)cmd.ExecuteScalar(); //读出数据库里一共有多少条数据   
  29.                
  30.             con.Close();   
  31.             DataBindlist();   
  32.         }   
  33.     }   
  34.   
  35.     protected void DataBindlist()   
  36.     {   
  37.           
  38.         SqlConnection con=new SqlConnection(ConfigurationSettings.AppSettings["SQLCONNECTION"].ToString());   
  39.         SqlDataAdapter ad = new SqlDataAdapter("select * from About", con);   
  40.         DataSet ds = new DataSet();   
  41.         ad.Fill(ds, pagelist.PageSize * (pagelist.CurrentPageIndex - 1), pagelist.PageSize, "About");   
  42.   
  43.         list.DataSource = ds.Tables["About"];   
  44.         list.DataBind();   
  45.       }   
  46.   
  47.     protected void PageChanging(object src,PageChangingEventArgs e)   
  48.     {   
  49.         pagelist.CurrentPageIndex = e.NewPageIndex;   
  50.         DataBindlist();   
  51.     }   
  52. }  
Code:
  1. //页面加载事件   
  2. protected void Page_Load(object sender, EventArgs e)   
  3. {   
  4. Bind();   
  5. }   
  6. private void Bind()   
  7. {   
  8. //将总条数放到分页控件中   
  9. DataTable dt1 = null;   
  10. dt1 = DataCtrl.selecttable("select count(classname) from mess_class","tabnl");   
  11. // this.pager.RecordCount=System.Convert.ToInt32(ds.Tables[0].Rows[0][0]);    
  12. this.AspNetPager1.RecordCount = System.Convert.ToInt32(dt1.Rows[0][0]);   
  13. Bind1();   
  14.   
  15. }   
  16. private void Bind1()   
  17. {   
  18. SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=xuedao;database=zhaoxuedaonet");   
  19. SqlCommand comm = new SqlCommand("select classname from mess_class",conn);   
  20. conn.Open();   
  21. comm.ExecuteNonQuery();   
  22.   
  23. DataSet ds = new DataSet();   
  24. SqlDataAdapter dapter = new SqlDataAdapter(comm);   
  25. dapter.Fill(ds, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex-1),AspNetPager1.PageSize,"table");   
  26. this.DataList1.DataSource = ds.Tables["table"].DefaultView;   
  27. this.DataList1.DataBind();   
  28. //AspNetPager1.CustomInfoText = "记录总数:" + AspNetPager1.RecordCount.ToString() + "";   
  29. //AspNetPager1.CustomInfoText = "总页数:" + AspNetPager1.PageCount.ToString() + "";   
  30. //AspNetPager1.CustomInfoText = " 当前页:" + AspNetPager1.CurrentPageIndex.ToString() + "";    
  31.   
  32. //myda.Fill(ds, pager.PageSize * (pager.CurrentPageIndex - 1), pager.PageSize, "article");   
  33. //this.dgList.DataSource = ds.Tables["article"];   
  34. //this.dgList.DataBind();   
  35. 动态设置用户自定义文本内容    
  36. //pager.CustomInfoText = "记录总数:" + pager.RecordCount.ToString() + "";   
  37. //pager.CustomInfoText += " 总页数:" + pager.PageCount.ToString() + "";   
  38. //pager.CustomInfoText += " 当前页:" + pager.CurrentPageIndex.ToString() + "";    
  39.   
  40.   
  41. }   
  42. //分页事件   
  43. protected void AspNetPager1_PageChanged(object src, Wuqi.Webdiyer.PageChangedEventArgs e)   
  44. {   
  45. AspNetPager1.CurrentPageIndex = e.NewPageIndex;   
  46. Bind1();   
  47. }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值