Repeater Paging 01

데이터 바인드 컨트롤에서 페이징은 아주 중요합니다. 특히 GridView는 자체로 페이징 기능이 있지만 Repeater은 없습니다. 하지만 효율면에서 볼때 Repeater는 GridView 보다 데이터가 많은 경우에 속도가 훨씬 빠릅니다. 그건 GridView가 가지고 있는 자체 기능이 워낙 많아서 컨트롤 자체가 무겁기 때문입니다. 아래는Repeater을 페이징 하는 간단한 DEMO 입니다.
=============================================================================================
Result:

=============================================================================================
DataBase is "Northwind". Used table is "Products". Add a ConnectionString in web.config.
Create a folder named "Pager". Add a webform named "Pager_Repeater01" under the folder.
=============================================================================================
Pager_Repeater01.aspx
====================

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

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

<script runat="server" language="C#">
    public void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
        string cmdText = "Select ProductName from Products";
        SqlDataAdapter sda = new SqlDataAdapter(cmdText, con);

        DataSet ds = new DataSet();
        sda.Fill(ds);

        PagedDataSource pds = new PagedDataSource();
        pds.DataSource = ds.Tables[0].DefaultView;
        pds.AllowPaging = true;
        pds.PageSize = 5;
        int curPage;

        if (Request.QueryString["Page"] != null)
            curPage = Convert.ToInt32(Request.QueryString["Page"]);
        else
            curPage = 1;

        pds.CurrentPageIndex = curPage - 1;
        lblCurrentPage.Text = "현재 페이지: " + curPage.ToString();

        if (!pds.IsFirstPage)
            lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(curPage - 1);
        if (!pds.IsLastPage)
            lnkNexe.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(curPage + 1);
        Repeater1.DataSource = pds;
        Repeater1.DataBind();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Repeater Paging01</title>
   
    <style>
        P,TD,DIV,SPAN {font-size:9pt}
    </style>
</head>
<body>
    <form id="form1" runat="server" method="post">
        <asp:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate>
            <table width="300px">
                <tr>
                    <td style="width:100%; color:Red">
                    <div style="background-color:#999999; font-size:11pt; color:White">
                    <center><strong>ProductName</strong></center>
                    </div>
                    </td>
                </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td style="width:100%">
                    <div style="padding:5px; color:#666666">
                        <%#DataBinder.Eval(Container.DataItem,"ProductName") %>
                    </div>
                </td>
            </tr>       
        </ItemTemplate>
        <FooterTemplate>
            </table> 
        </FooterTemplate>
        </asp:Repeater>
    -------------------------------------------------<br />
    <div>
        <asp:Label ID="lblCurrentPage" runat="server"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:HyperLink ID="lnkPrev" runat="server">이전</asp:HyperLink>&nbsp;&nbsp;
        <asp:HyperLink ID="lnkNexe" runat="server">다음</asp:HyperLink>
    </div>
    </form>
</body>
</html>

posted @ 2009-03-13 18:08  OOK  阅读(414)  评论(0编辑  收藏  举报