asp: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文件代码:

   
   
   
   
   

           
           
           
   
hehe
  
      
       
       
       
       
       
头模板
<%#Eval("timekey")%>
<%#Eval("SalesAmountQuota")%>
尾模板

       
       

     首页
     下一页
     上一页
     末页
    
当前页为:
                    Text="Label">

               

                共
                页

第二种方式:
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" %>





    无标题页


   

   

   
       
           
头模板
序号:<%# Eval("ProductKey") %>
编码:<%# Eval("ProductAlternateKey") %>
脚模板

           
            当前页:
           

           
           
       
       

       

   
   

   




你可能感兴趣的:(ASP.NET(C#))