ASP.NETラーニングのページングデータバインド_Repeater+AspNetPager

17078 ワード

準備:
1、AspNetPagerをダウンロードする.dllファイル
2、プロジェクトファイルの下のbinファイルにコピーする
3、プロジェクトの参照を見つけて、右クリックで参照を追加して、ソリューションの中でファイルを変更していない場合、プレビューして、そのファイルを見つけて、「OK」をクリックします.このファイルをプロジェクトに参照します.
4、AspNetPagerコンポーネントを使用するページの上部にこのコンポーネントを登録する:登録コードは以下の通り:
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>

フロントページ:
 
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Repeater  .aspx.cs" Inherits="      .Repeater  " %>
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer"%>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <style type="text/css">
    /*    */
    .anpager .cpb {background:#1F3A87 none repeat scroll 0 0;border:1px solid #CCCCCC;color:#FFFFFF;font-weight:bold;margin:5px 4px 0 0;padding:4px 5px 0;}
    .anpager a {background:#FFFFFF none repeat scroll 0 0;border:1px solid #CCCCCC;color:#1F3A87;margin:5px 4px 0 0;padding:4px 5px 0;text-decoration:none}
    .anpager a:hover{background:#1F3A87 none repeat scroll 0 0;border:1px solid #1F3A87;color:#FFFFFF;}
 
    /*     */
    .paginator { font: 11px Arial, Helvetica, sans-serif;padding:10px 20px 10px 0; margin: 0px;}
    .paginator a {padding: 1px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none;margin-right:2px}
    .paginator a:visited {padding: 1px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none;}
    .paginator .cpb {padding: 1px 6px;font-weight: bold; font-size: 13px;border:none}
    .paginator a:hover {color: #fff; background: #ffa501;border-color:#ffa501;text-decoration: none;}
 
    /*    */
    .pages { color: #999 }
    .pages a, .pages .cpb { text-decoration:none;float: left; padding: 0 5px; border: 1px solid #ddd;background: #ffff;margin:0 2px; font-size:11px; color:#000;}
    .pages a:hover { background-color: #E61636; color:#fff;border:1px solid #E61636; text-decoration:none;}
    .pages .cpb { font-weight: bold; color: #fff; background: #E61636; border:1px solid #E61636;}
 
    .code{font-weight:bold;color:blue}
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Repeater ID="Repeater1" runat="server">
                <HeaderTemplate>
                    <table>
                        <tr>
                            <th>
                                  ID
                            </th>
                            <th>
                                  
                            </th>
                            <th>
                                  
                            </th>
                        </tr>
                </HeaderTemplate>
                <ItemTemplate>
                    <tr>
                            <th>
                                <%#Eval("Uid") %>
                            </th>
                            <th>
                                <%#Eval("Subject") %>
                            </th>
                            <th>
                                <%#Eval("Content") %>
                            </th>
                    </tr>
                </ItemTemplate>
                <FooterTemplate>
                    </table>
                </FooterTemplate>
            </asp:Repeater>
            <webdiyer:AspNetPager ID ="AspNetPager" runat="server" 
                FirstPageText="  " LastPageText ="  " NextPageText="   " 
                OnPageChanging="AspNetPager_PageChanging" PrevPageText="   "
                CssClass="anpager" PageSize="2">
            </webdiyer:AspNetPager>
        </div>
    </form>
</body>
</html>

バックグラウンドコード:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace       
{
    public partial class Repeater   : System.Web.UI.Page
    {

        //           
        string StrCon = ConfigurationManager.ConnectionStrings["MyDB"].ToString(); 
        protected void Page_Load(object sender, EventArgs e)
        {
           if(!IsPostBack){
               BindData();
           }
        }

        /// <summary>
        ///                
        /// </summary>
        private void BindData()
        {
            SqlConnection con = new SqlConnection(StrCon);  //         
            con.Open();   //       
            SqlDataAdapter sqlda = new SqlDataAdapter("select * from tb_LeaveWord", con);  //         
            DataSet ds = new DataSet();
            sqlda.Fill(ds);    // dateSet     【  SQLDateAdapter   DataSet      】
            //【  :】                ,     DataSet 

            //              【      【           】   】
            //                    ,      ,               ,              
            PagedDataSource pdsList = new PagedDataSource();
            pdsList.DataSource = ds.Tables[0].DefaultView;  //      
            pdsList.AllowPaging = true;//       
            pdsList.PageSize = this.AspNetPager.PageSize;//        
            pdsList.CurrentPageIndex = this.AspNetPager.CurrentPageIndex - 1;//     
            
            //           
            this.AspNetPager.RecordCount = ds.Tables[0].Rows.Count;//    
            this.AspNetPager.PageSize = 2;    //         
            //             
            this.Repeater1.DataSource = pdsList;   //            
            this.Repeater1.DataBind();    //    
        }

        /// <summary>
        ///       
        /// </summary>
        /// <param name="src"></param>
        /// <param name="e"></param>
        public void AspNetPager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            AspNetPager.CurrentPageIndex = e.NewPageIndex;
            BindData();
        }

    }
}

注:バックグラウンドコードには、次の2つの点に注意してください.
第一:これを書くのを忘れてはいけない.AspNetPager.RecordCount = ds.Tables[0].Rows.Count;//合計を記録するこのコードは、否定者ページングコンポーネントが表示されません.
第二:必ずAspNetPagerを書くCurrentPageIndex = e.NewPageIndex;このコードは、ページ数に対応する表示データが乱れています.