[ASP.NET2.0]Repeater C# 分页用法

 转自:http://www.cnblogs.com/xiediy/archive/2008/09/09/1287567.html

 

一、预览效果

 

二、Web.config配置

1    < appSettings >
2      < add  key ="haikelasiConnectionString"  value ="Data Source=.;Initial Catalog=haikelasi;Persist Security Info=True;User ID=sa;Password=123456"   />
3    </ appSettings >

 

三、前台控件呈现部分

 1 < asp:repeater  id ="LeaveMessage"  runat ="server"   >
 2 < ItemTemplate >
 3 < table  width ="100%"  border ="0"  align ="center"  cellpadding ="1"  cellspacing ="1"  bgcolor ="#D4D0C8" >
 4 < tr >
 5 < td  width ="85%"  bgcolor ="#FFFAFF" >< div  align ="left" > <% #DataBinder.Eval(Container.DataItem,  " sNewsTitle " ) %> </ div ></ td >
 6 < td  width ="15%"  bgcolor ="#FFFAFF"  align ="left" > <% #DataBinder.Eval(Container.DataItem,  " dAddTime " ) %> </ td >
 7 </ tr >
 8 </ table >
 9 < hr  size ="3px"  width ="90%" />
10 </ ItemTemplate >  
11 </ asp:repeater >
12 共有 < asp:Literal  ID ="RecordCount"  runat ="server" ></ asp:Literal > 条记录
13 共有 < asp:Literal  ID ="PageCount"  runat ="server" ></ asp:Literal >
14 当前第 < asp:Literal  ID ="Pageindex"  runat ="server" ></ asp:Literal >
15 < asp:HyperLink  ID ="FirstPage"  runat ="server"  Text ="首页" ></ asp:HyperLink >
16 < asp:HyperLink  ID ="PrevPage"  runat ="server"  Text ="上一页" ></ asp:HyperLink >
17 < asp:HyperLink  ID ="NextPage"  runat ="server"  Text ="下一页" ></ asp:HyperLink >
18 < asp:HyperLink  ID ="LastPaeg"  runat ="server"  Text ="尾页" ></ asp:HyperLink >
19 跳转到 < asp:Literal  ID ="Literal1"  runat ="server" ></ asp:Literal >

 

四、后置代码部分(CS代码)

  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  System.Text;
 13
 14 public   partial   class  admin_LeaveMessages : System.Web.UI.Page
 15 {
 16      protected   void  Page_Load( object  sender, EventArgs e)
 17      {
 18          if  ( ! Page.IsPostBack)
 19          {
 20             NewsBind();
 21         }

 22     }

 23      private   void  NewsBind() // repeater分页并绑定
 24      {
 25          string  SqlStr  =   " select sNewsTitle,dAddTime from [News] order by dAddTime " ;        
 26          string  connectionString  =  System.Configuration.ConfigurationManager.AppSettings[ "
haikelasiConnectionString " ].ToString();
 27         SqlConnection conn  =   new  SqlConnection(connectionString);
 28         conn.Open();
 29         SqlDataAdapter Adapter  =   new  SqlDataAdapter(SqlStr, conn);
 30         DataSet ds  =   new  DataSet();
 31          try
 32          {
 33             Adapter.Fill(ds,  " testTable " );
 34             PagedDataSource objPage  =   new  PagedDataSource();
 35             objPage.DataSource = ds.Tables[ " testTable " ].DefaultView;
 36             objPage.AllowPaging = true ;
 37             objPage.PageSize = 3 ;
 38              int  CurPage;
 39              if  (Request.QueryString[ " Page " !=   null )
 40              {
 41                 CurPage  =  Convert.ToInt32(Request.QueryString[ " page " ]);
 42             }

 43              else
 44              {
 45                 CurPage  =   1 ;
 46             }

 47             objPage.CurrentPageIndex  =  CurPage  -   1 ;
 48             LeaveMessage.DataSource = objPage; // 这里更改控件名称
 49             LeaveMessage.DataBind(); // 这里更改控件名称
 50             RecordCount.Text  =  objPage.DataSourceCount.ToString();
 51             PageCount.Text  =  objPage.PageCount.ToString();
 52             Pageindex.Text  =  CurPage.ToString();
 53             Literal1.Text  =  PageList(objPage.PageCount, CurPage);
 54              // Literal1.Text = PageList(objPage.PageCount, Pageindex, L_Manage);  // 带参数的:LManage为参数
 55
 56
 57             FirstPage.NavigateUrl  =  Request.CurrentExecutionFilePath  +   " ?page=1 " ;
 58             PrevPage.NavigateUrl  =  Request.CurrentExecutionFilePath  +   " ?page= "   +  (CurPage  -   1 );
 59             NextPage.NavigateUrl  =  Request.CurrentExecutionFilePath  +   " ?page= "   +  (CurPage  +   1 );           
 60             LastPaeg.NavigateUrl  =  Request.CurrentExecutionFilePath  +   " ?page= "   +  objPage.PageCount.ToString();
 61              if  (CurPage  <=   1   &&  objPage.PageCount  <=   1 )
 62              {
 63                 FirstPage.NavigateUrl  =   "" ;
 64                 PrevPage.NavigateUrl  =   "" ;
 65                 NextPage.NavigateUrl  =   "" ;
 66                 LastPaeg.NavigateUrl  =   "" ;
 67                  /**/ /*
 68                 FirstPage.Visible = false;
 69                 PrevPage.Visible = false;
 70                 NextPage.Visible = false;
 71                 LastPaeg.Visible = false;
 72                  */

 73             }

 74              if  (CurPage  <=   1   &&  objPage.PageCount  >   1 )
 75              {
 76                 FirstPage.NavigateUrl  =   "" ;
 77                 PrevPage.NavigateUrl  =   "" ;
 78                  /**/ /*
 79                 FirstPage.Visible = false;
 80                 PrevPage.Visible = false;
 81                  */

 82
 83
 84             }

 85              if  (CurPage  >=  objPage.PageCount)
 86              {
 87                 NextPage.NavigateUrl  =   "" ;
 88                 LastPaeg.NavigateUrl  =   "" ;
 89                  /**/ /*
 90                 NextPage.Visible = false;
 91                 LastPaeg.Visible = false;
 92                  */

 93             }

 94         }

 95          catch (Exception error)
 96          {
 97             Response.Write(error.ToString());
 98         }

 99          finally
100          {
101             conn.Close();
102         }

103     }

104      private   string  PageList( int  Pagecount,  int  Pageindex) // private string Jump_List(int Pagecount , int Pageindex , long L_Manage) // 带参数的传递
105      {
106         StringBuilder sb  =   new  StringBuilder();
107          // 下为带参数的传递
108          // sb.Append("<select id=/"Page_Jump/" name=/"Page_Jump/" οnchange=/"window.location='" + Request.CurrentExecutionFilePath + "?page='+ this.options[this.selectedIndex].value + '&Org_ID=" + L_Manage + "';/">");
109          // 不带参数的传递
110         sb.Append( " <select id=/ " Page_Jump/ "  name=/ " Page_Jump/ "  οnchange=/ " window.location = ' " + Request.CurrentExecutionFilePath + "?page= ' +   this .options[ this .selectedIndex].value  +   '' ;/ " > " );
111
112          for  ( int  i  =   1 ; i  <=  Pagecount; i ++ )
113          {
114              if  (Pageindex  ==  i)
115                 sb.Append( " <option value=' "   +  i  +   " ' selected> "   +  i  +   " </option> " );
116              else
117                 sb.Append( " <option value=' "   +  i  +   " '> "   +  i  +   " </option> " );
118         }

119         sb.Append( " </select> " );
120          return  sb.ToString();
121     }

122
123    
124 }

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值