天天看點

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檔案代碼:

<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 οnmοusedοwn="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" οnclick="BtnUp_Click" Text="上一頁" />

            <asp:Button ID="BtnDown" runat="server" οnclick="BtnDown_Click" Text="下一頁" />

        </asp:Panel>

        <br />

        <br />

    </div>

    </form>

</body>

</html>

繼續閱讀