天天看點

ASP.NET(C#) 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()

    {

           sqlconnection con = new sqlconnection("server='.';database=test;uid=sa;pwd=123456;");

        sqlconnection conn = new sqlconnection();

        dataset ds = new dataset();

        sqldataadapter sda = new sqldataadapter("select * from news", con);

        sda.fill(ds, "newstitle");

        //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);

        }

            this.first.visible = false ;

            this.last.visible = false ;

        if (!pds.islastpage)

            next.navigateurl = request.currentexecutionfilepath + "?page=" + convert.tostring(curpage + 1);

            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" > <%#("timekey")%></font></td></tr></itemtemplate>

        <alternatingitemtemplate ><tr><td > <a href ='default.aspx?id=<%#"databaselogid" %>'><%#("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> 

                共<asp:label id="labpage" runat="server" text="label"></asp:label>

                頁</td><td class="style1" style="height: 21px">

       <asp:hyperlink id="next" runat="server">下一頁</asp:hyperlink>

    </table>

第二種方式:

using system.collections;

using system.linq;

using system.xml.linq;

public partial class databind : system.web.ui.page

            num.text = "1";

            repdatabind();

    public void repdatabind()

        sqlconnection con = new sqlconnection("server='.';database=test;uid=sa;pwd=123456;");

        sqldataadapter sda = new sqldataadapter("select * from dimproduct", con);

        sda.fill(ds, "name");

        int curpage = convert.toint32(num.text);

        this.btndown.enabled = true;

        this.btnup.enabled = true;

        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) ;

aspx代碼:

<%@ page language="c#" 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>序号:<%# ("productkey") %></td></tr><tr><td>編碼:<%# ("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 />

    </div>

    </form>

</body>

</html>

繼續閱讀