Repeater控件分页例子(ASP.NET 2.0)

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;

public  partial  class  _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
            RepeaterDataBind();
    }


    
private void RepeaterDataBind()
    
{
               
string connectionString = ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
               SqlConnection conn 
= new SqlConnection(connectionString);
       
// Configuration rootWebConfig =
        conn.Open();
        SqlDataAdapter Adapter 
= new SqlDataAdapter("select firstname, lastname from Employees", conn);
        DataSet ds  
= new DataSet();
        
try
        
{
            Adapter.Fill(ds, 
"testTable");
            PagedDataSource objPage 
= new PagedDataSource();
            objPage.DataSource 
= ds.Tables["testTable"].DefaultView;
            objPage.AllowPaging 
= true;
            objPage.PageSize 
= 5;
            
int CurPage;
            
if(Request.QueryString["Page"!= null)
                CurPage 
= Convert.ToInt32(Request.QueryString["Page"]);
            
else
                CurPage 
= 1;
            objPage.CurrentPageIndex 
= CurPage - 1;
            lblCurPage.Text 
= "当前第" + CurPage.ToString()+"";

            
if(!objPage.IsFirstPage)
                lnkPrev.NavigateUrl
=Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
            
if(!objPage.IsLastPage)
                lnkNext.NavigateUrl
=Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
            RepeaterPage.DataSource 
= objPage;
            RepeaterPage.DataBind();

        }

        
catch(Exception error)
        
{
            Response.Write(error.ToString());
        }

        
finally
        
{
            conn.Close();
        }

    }

}

你可能感兴趣的:(asp.net)