zoukankan      html  css  js  c++  java
  • ASP.NET学习之分页数据绑定_Repeater+AspNetPager

    准备工作:

    1、下载AspNetPager.dll文件

    2、将其拷贝到项目文件下的bin文件里

    3、找到项目的引用,右键添加引用,如果解决方案中没有改文件,就预览,找到该文件,点击确定。将该文件引用到项目中来。

    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();
            }
    
        }
    }

    注意:在后台代码中有两点需要注意:

    第一:一定不能忘记写this.AspNetPager.RecordCount = ds.Tables[0].Rows.Count;//记录总数这句代码,否者分页组件显示不出来

    第二:一定要写 AspNetPager.CurrentPageIndex = e.NewPageIndex;这句代码,否则页数对应的显示数据是乱的。

  • 相关阅读:
    Mysql常用sql&索引失效场景
    Linux命令demo
    js中浮点数的精度问题
    Linux运行程序出现Cannot execute binary file
    我的第一篇博客文(抽象类练习)
    关于MyEclipse中build path和compiler的jre不匹配
    jQuery操作单选框、多选框是否选中问题
    oracle索引操作
    JS组件多选下拉框Bootstrap之select2 使用心得
    MyEclipse10——java项目clean重新编译
  • 原文地址:https://www.cnblogs.com/Yisijun/p/4589152.html
Copyright © 2011-2022 走看看