Repeaterページングコード

21925 ワード

//ASP.NET  DataList Repeater             ,  ,      <ItemTemplate>                    !
//.        Datagrid  ,        。
//
////
//       【PagedDataSource】          。
//
//PagedDataSource    :
//
//
//DataSource -   
//
//AllowPaging - true       .
//PageSize -       
//PageCount -    
//
//CurrentPageIndex -         
// Repeater       :
==========================================================================
//Result.aspx.cs   
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace comeday
{
 /// <summary>
 /// Result      。
 /// </summary>
 public class Result : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.HyperLink lnkPrev;
  protected System.Web.UI.WebControls.HyperLink lnkNext;
  protected System.Web.UI.WebControls.Label lblCurrentPage;
  protected System.Web.UI.WebControls.Label lblTotalPage;
  protected System.Web.UI.WebControls.Repeater Repeater1;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
//      
   PagedDataSource Pgds=new PagedDataSource();
//        
   Pgds.DataSource=CreateDataSource().DefaultView;
//              
   Pgds.AllowPaging=true;
//             6 
   Pgds.PageSize=6;
//              
//
   lblTotalPage.Text=Pgds.PageCount.ToString();
//           
   int CurrentPage;
//               null     ,      
   if(Request.QueryString["Page"] != null)
   {
    
    CurrentPage = Convert.ToInt32(Request.QueryString["Page"]);
   }
   else
   {
    CurrentPage = 1;
   }
//           -1
   Pgds.CurrentPageIndex = CurrentPage - 1;
//         
   lblCurrentPage.Text = CurrentPage.ToString();
//          ,    Page         -1,       
   if(!Pgds.IsFirstPage)
   {
    //            Request.CurrentExecutionFilePath         
    lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurrentPage - 1);
   }
//        End If
//           ,    Page         +1,       
   if(!Pgds.IsLastPage)
   {
//    Request.CurrentExecutionFilePath         
    lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurrentPage + 1);
   }
//            
   Repeater1.DataSource = Pgds;
   Repeater1.DataBind();
  }
  /// <summary>
  ///      (  Datatable)
  /// </summary>
  /// <returns></returns>
public DataTable CreateDataSource()
{
  DataTable dt; 
  DataRow dr;
  dt=new DataTable();
 //   
        dt.Columns.Add(new DataColumn("IntegerValue", System.Type.GetType("System.Int32")));
        dt.Columns.Add(new DataColumn("StringValue", System.Type.GetType("System.String")));
        dt.Columns.Add(new DataColumn("DateTimeValue", System.Type.GetType("System.DateTime")));
        dt.Columns.Add(new DataColumn("BoolValue", System.Type.GetType("System.Boolean")));
 //    
 for( int i=0;i<50;i++)
 {
  dr = dt.NewRow();
  dr[0] = i;
  dr[1] = "Item " + i.ToString();
  dr[2] = DateTime.Now.ToShortTimeString();
  if(i%2!=0)
  {
   dr[3] = true;
  }
  else
  {
   dr[3] = false;
  }
  dt.Rows.Add(dr);
 }
 return dt;
}
  #region Web           
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN:      ASP.NET Web          。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  ///            -            
  ////// </summary>
  private void InitializeComponent()
  {    
   this.Load += new System.EventHandler(this.Page_Load);
  }
  #endregion
 }
}
===========================================================================
Result.aspx  
<%@ Page Language="c#" Codebehind="Result.aspx.cs" AutoEventWireup="false" Inherits="comeday.Result" %>
<%@ import Namespace="System.Data" %>
<HTML>
<HEAD>
<title>DataRepeater</title>
<style type="text/css">

BODY 
{
 FONT: 10px Verdana, Arial, Helvetica, "sans serif"; COLOR: #000000 }

.txt { FONT-SIZE: 12px }

</style>
</HEAD>
<body>
<form id="Form2" name="form1" method="post" runat="server">
<table class="txt" width="100%" border="0">
     <tbody>
     <tr>
     <td>
     <asp:hyperlink id="lnkPrev" runat="server">   </asp:hyperlink>
     <asp:hyperlink id="lnkNext" runat="server">   </asp:hyperlink> <asp:label id="lblCurrentPage" runat="server"></asp:label>
        <asp:label id="lblTotalPage" runat="server"></asp:label>
</td> </tr> </tbody> </table> <asp:repeater id="Repeater1" runat="server"> <ItemTemplate> <hr align="left" width="60%" size="1"> <table class="txt" width="100%" border="0"> <tr> <td> Order Date: <%# DataBinder.Eval(Container.DataItem, "DateTimeValue", "{0:d}") %> </td> <tr> <td> Quantity: <%# DataBinder.Eval(Container.DataItem, "IntegerValue", "{0:N2}") %> </td> <tr> <td> Item: <%# DataBinder.Eval(Container.DataItem, "StringValue") %> </td> <tr> <td> Order Date: <asp:checkbox id=chk1 checked='<%# DataBinder.Eval(Container.DataItem, "BoolValue") %>' runat="server"/> </td> </tr> </table> </ItemTemplate> </asp:repeater> <hr hight="1"> </form> </body> </HTML>

<%@ Page Language="c#" Codebehind="Result.aspx.cs" AutoEventWireup="false" Inherits="comeday.Result" %>

DataList 。