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

16014 ワード

基本的にはRepeater+AspNetPagerと同じです.
フロントページ:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridView+AspNetPager.aspx.cs" Inherits="      .GridView_AspNetPager" %>
<%@ 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:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField HeaderText="  ID">
                    <ItemTemplate><%#Eval("Uid") %></ItemTemplate>
                </asp:TemplateField>
                 <asp:TemplateField HeaderText="  ">
                     <ItemTemplate><%#Eval("Subject") %></ItemTemplate>
                 </asp:TemplateField>
                 <asp:TemplateField HeaderText="  ">
                     <ItemTemplate><%#Eval("Content") %></ItemTemplate>
                 </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <webdiyer:AspNetPager ID="AspNetPager" runat="server" FirstPageText="  "
            PrevPageText="   " LastPageText="  " NextPageText="   " 
            OnPageChanging="AspNetPager_PageChanged" CssClass="paginator" PageSize="5">
        </webdiyer:AspNetPager>
    </div>
    </form>
</body>
</html>

バックグラウンドコード:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace       
{
    public partial class GridView_AspNetPager : System.Web.UI.Page
    {
        string sqlStr = ConfigurationManager.ConnectionStrings["MyDB"].ToString();//           
        protected void Page_Load(object sender, EventArgs e)
        {
            //     
            if (!IsPostBack)
            {
                ShowData();
            }
        }

        /// <summary>
        ///            
        /// </summary>
        /// <returns>    </returns>
        public DataSet GetData()
        {
            using (SqlConnection sqlConn = new SqlConnection(sqlStr))
            {
                string sqlqueryStr = "select * from tb_LeaveWord";
                SqlDataAdapter sda = new SqlDataAdapter(sqlqueryStr, sqlConn);
                DataSet ds = new DataSet();
                sda.Fill(ds);
                return ds;
            }
        }

        /// <summary>
        ///     
        /// </summary>
        public void ShowData()
        {
            DataSet ds = GetData();
            //   
            PagedDataSource pds = new PagedDataSource();
            pds.DataSource = ds.Tables[0].DefaultView;  //     
            pds.AllowPaging = true;
            pds.PageSize = this.AspNetPager.PageSize;   //【   】        ,   Asp.NetPager PageSize
            pds.CurrentPageIndex = this.AspNetPager.CurrentPageIndex - 1;

            //                  
            this.AspNetPager.RecordCount = ds.Tables[0].Rows.Count;
            this.AspNetPager.PageSize = 5;

            //            【       】【   :          】
            GridView1.DataSource = pds;
            //        
            GridView1.DataBind();
        }

        /// <summary>
        ///          
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager_PageChanged(object sender, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            AspNetPager.CurrentPageIndex = e.NewPageIndex;
            ShowData();
        }
    }
}