repeater分页的实现

第一种方式:

数据库连接代码:

using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  System.Data.SqlClient;
using  System.Data.Sql;



public   partial   class  _Default : System.Web.UI.Page
{
    
private void  con()
    
{
        
string connstring = ConfigurationManager.ConnectionStrings["AdventureWorksDWConnectionString"].ConnectionString;
        SqlConnection con 
= new SqlConnection(connstring);
        SqlConnection conn 
= new SqlConnection();
        DataSet ds 
= new DataSet();
        SqlDataAdapter sda 
= new SqlDataAdapter("select * from FactSalesQuota", con);
        sda.Fill(ds, 
"name");
        SqlDataAdapter sda2 
= new SqlDataAdapter("select * from ProspectiveBuyer", con);
        sda2.Fill(ds, 
"title");
        PagedDataSource pds 
= new PagedDataSource();
        pds.DataSource 
= ds.Tables["name"].DefaultView;
        
//PagedDataSource aa = new PagedDataSource();
        pds.AllowPaging = true;//允许分页
        pds.PageSize = 8;//单页显示项数
        int CurPage;
        
if (Request.QueryString["Page"!= null)
            CurPage 
= Convert.ToInt32(Request.QueryString["Page"]);
        
else
            CurPage 
= 1;
        pds.CurrentPageIndex 
= CurPage - 1;
        
int Count = pds.PageCount;

        lblCurrentPage.Text 
= "当前页:" + CurPage.ToString();
        labPage.Text 
= Count.ToString();

        
if (!pds.IsFirstPage)
        
{
            
this.first.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";
            
this.last.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(Count  - 1); ;
            up.NavigateUrl 
= Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
        }

        
else
        
{
            
this.first.Visible = false ;
            
this.last.Visible = false ;
 
        }


        
if (!pds.IsLastPage)
        
{
           
  
            next.NavigateUrl 
= Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
        }

        
else
        
{
            
this.first.Visible = false;
            
this.last.Visible = false;

        }


        Repeater1.DataSource 
= pds ;
        Repeater1.DataBind();

    }




    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            con();
            
this.first.Visible = true;
            
this.last.Visible = true;
            
//this.Repeater1.DataSource = pds();
            
//this.Repeater1.DataBind();

        }


    }

}


aspx文件代码:

    < table >
    
< tr  >< td  class = " style1 "  align  = " left "    > hehe td > tr >
    
    
< tr  >< td  class = " style1 " >    
       
< asp:Repeater ID = " Repeater1 "  runat = " server "   >
        
        
< HeaderTemplate  >< table >< tr >< td > 头模板 td > tr > HeaderTemplate >
        
< ItemTemplate  >< tr >< td  >< font color = " red "   >   <% #Eval( " timekey " ) %> font > td > tr > ItemTemplate >
        
< AlternatingItemTemplate  >< tr >< td  >   < a href  = ' Default.aspx?id=<%#"databaselogid" %> ' ><% #Eval( " SalesAmountQuota " ) %> a > td > tr > AlternatingItemTemplate >
        
< FooterTemplate  >< tr >< td > 尾模板 td > tr > table > FooterTemplate >
        
asp:Repeater >  
        
td >   tr >  
    
    
 
< tr >   < td  class = " style1 " >
     
< asp:HyperLink ID = " first "  runat = " server " > 首页 asp:HyperLink >
     
< asp:HyperLink ID = " next "  runat = " server " > 下一页 asp:HyperLink >
     
< asp:HyperLink ID = " up "  runat = " server " > 上一页 asp:HyperLink >
     
< asp:HyperLink ID = " last "  runat = " server " > 末页 asp:HyperLink >
     
td > tr >
            
            
< tr >< td  class = " style1 " > 当前页为: < asp:Label ID = " lblCurrentPage "  runat = " server "  
                    Text
= " Label " > asp:Label >
                
< br  />
                共
< asp:Label ID = " labPage "  runat = " server "  Text = " Label " > asp:Label >
                页
td > tr >
            
    
table >

第二种方式:

using  System;
using  System.Collections;
using  System.Configuration;
using  System.Data;
using  System.Linq;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.HtmlControls;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Xml.Linq;
using  System.Data.SqlClient;

public   partial   class  databind : System.Web.UI.Page
{
   
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            num.Text 
= "1";
            repdatabind(); 
        }



    }

    
public void repdatabind()
    
{
        
string connstring = ConfigurationManager.ConnectionStrings["AdventureWorksDWConnectionString"].ConnectionString;
        SqlConnection con 
= new SqlConnection(connstring);
        SqlConnection conn 
= new SqlConnection();
        DataSet ds 
= new DataSet();
        SqlDataAdapter sda 
= new SqlDataAdapter("select * from DimProduct", con);
        sda.Fill(ds, 
"name");
        PagedDataSource pds 
= new PagedDataSource();
        pds.DataSource 
= ds.Tables["name"].DefaultView;
        pds.AllowPaging 
= true;//允许分页
        pds.PageSize = 8;//单页显示项数

        
int curpage = Convert.ToInt32(num.Text);
        
this.BtnDown.Enabled = true;
        
this.BtnUp.Enabled = true;
        pds.CurrentPageIndex 
= curpage - 1;
        
if (curpage == 1)
        
{
            
this.BtnUp.Enabled = false;
        }

        
if (curpage == pds.PageCount)
        
{
            
this.BtnDown.Enabled = false;
        }

        
this.Repeater1.DataSource = pds;
        
this.Repeater1.DataBind();
    }

 
    
protected void BtnUp_Click(object sender, EventArgs e)
    
{
        
this.num.Text =Convert.ToString ( Convert.ToInt32(num.Text)- 1) ;
        repdatabind();
    }

    
protected void BtnDown_Click(object sender, EventArgs e)
    
{
        
this.num.Text = Convert.ToString(Convert.ToInt32(num.Text)+ 1) ;
        repdatabind();
    }

}

aspx代码:

<% @ Page Language = " C# "  AutoEventWireup = " true "  CodeFile = " databind.aspx.cs "  Inherits = " databind "   %>

DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >

< html xmlns = " http://www.w3.org/1999/xhtml " >
< head runat = " server " >
    
< title > 无标题页 title >
head >
< body >
    
< form id = " form1 "  runat = " server " >
    
< div >
    
        
< asp:Panel ID = " Panel1 "  runat = " server "  Height = " 173px " >
            
< asp:Repeater ID = " Repeater1 "  runat = " server " >< HeaderTemplate  >< table border onmousedown = " 1 "   >< tr >< td  > 头模板 td > tr > HeaderTemplate >< ItemTemplate  >< tr >< td > 序号: <% # Eval( " ProductKey " %> td > tr >< tr >< td > 编码: <% # Eval( " ProductAlternateKey " %> td > tr > ItemTemplate >< FooterTemplate  >< tr >< td > 脚模板 td > tr > table > FooterTemplate >
            
asp:Repeater >
            当前页:
< asp:Label ID = " num "  runat = " server " > asp:Label >
            
< br  />
            
< asp:Button ID = " BtnUp "  runat = " server "  onclick = " BtnUp_Click "  Text = " 上一页 "   />
            
< asp:Button ID = " BtnDown "  runat = " server "  onclick = " BtnDown_Click "  Text = " 下一页 "   />
        
asp:Panel >
        
< br  />
        
< br  />
    
    
div >
    
form >
body >
html >

你可能感兴趣的:(repeater分页的实现)