Repeater Paging 02

아래 대모는 Repeater Paging 01과 비슷하게 PagedDataSource를 사용 해서 페이징을 한 것이지만 Repeater를 두개를 사용하요 하나는 페이저로 사용하고 다른 하나는 데이터 바인드 컨트롤로 사용 하였습니다. 아래는 소스를 첨부 합니다.
============================================================================================
Used DB is "Northwind". Used table is "Products". Add a connectionString in web.config.
Result:

=============================================================================================
Source:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Collections" %>
<%@ 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">
    public int PageNumber
    {
        get
        {
            if (ViewState["PageNumber"] != null)
                return Convert.ToInt32(ViewState["PageNumber"]);
            else
                return 0;
        }
        set
        {
            ViewState["PageNumber"] = value;
        }
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        rptPages.ItemCommand +=
           new RepeaterCommandEventHandler(rptPages_ItemCommand);
    }
   
    public void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            GetData();
    }

    private void GetData()
   {
      SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
      SqlDataAdapter da = new SqlDataAdapter("select ProductName from Products", con);
      DataTable dt = new DataTable();
      da.Fill(dt);
      PagedDataSource pds = new PagedDataSource();
      DataView dv = new DataView(dt);
      pds.DataSource = dv;
      pds.AllowPaging = true;
      pds.PageSize = 10;
      pds.CurrentPageIndex = PageNumber;
      if (pds.PageCount > 1)
      {
         rptPages.Visible = true;
         ArrayList pages = new ArrayList();
         for (int i = 0; i < pds.PageCount; i++)
         pages.Add((i + 1).ToString());
         rptPages.DataSource = pages;
         rptPages.DataBind();
      }
      else
          rptPages.Visible = false;
      rptItems.DataSource = pds;
      rptItems.DataBind();
   }
   
   void rptPages_ItemCommand(object source,RepeaterCommandEventArgs e)
   {
      PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
      GetData();
   }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Pager_Repeater02</title>
   
    <style>
        P,TD,DIV,SPAN {font-size:9pt}
    </style>
</head>
<body>
    <form id="form1" runat="server" method="post">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>      
        <asp:Repeater ID="rptPages" Runat="server">
      <HeaderTemplate>
      <table cellpadding="0" cellspacing="0" border="0">
      <tr>
         <td><b>Page:</b>&nbsp;</td>
         <td>
      </HeaderTemplate>
      <ItemTemplate>
         <asp:LinkButton ID="btnPage"
                         CommandName="Page"
                         CommandArgument="<%#Container.DataItem %>" Runat="server"><%# Container.DataItem %>
                         </asp:LinkButton>&nbsp;
      </ItemTemplate>
      <FooterTemplate>
         </td>
      </tr>
      </table>
      </FooterTemplate>
      </asp:Repeater>
      <asp:Repeater ID="rptItems" runat="server">
      <HeaderTemplate>
      <ul>
      </HeaderTemplate>
      <ItemTemplate>
      <li><%# Eval("ProductName") %></li>
      </ItemTemplate>
      <FooterTemplate>
      </ul>
      </FooterTemplate>
      </asp:Repeater>
      </ContentTemplate>
    </asp:UpdatePanel>
    </form>
</body>
</html>

 

posted @ 2009-03-16 13:14  OOK  阅读(238)  评论(0编辑  收藏  举报