zoukankan      html  css  js  c++  java
  • ASP.NET的分页方法(二)

    第二讲主要使用到了常用的分页控件aspnetpager,这里对他就行一个简单的应用,具体大家可以到杨涛的博客上去寻找相关的DLL,

    首先要先引用AspNetPager.dll,然后把这个DLL同时添加入工具箱

    接下来前台依然使用repeater控件进行绑定,写法如下:

    <form id="form1" runat="server">
            <div>
                <ul style="list-style: none">
                    <asp:Repeater ID="Repeater1" runat="server">
                        <ItemTemplate>
                            <li><%#Eval("Title") %></li>
                        </ItemTemplate>
                    </asp:Repeater>
                </ul>
            </div>
            <webdiyer:AspNetPager ID="AspNetPager1"  CssClass="paginator" runat="server" OnPageChanged="AspNetPager1_PageChanged">
            </webdiyer:AspNetPager>
        </form>

    好看的样式网上也有三种左右(我目前所找到的),其中一种如下:

    <style type="text/css">
            /*拍拍网风格*/
            .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;
                }
        </style>

    后台的写法 如下,使用的是dataset绑定的数据源,但是似乎没有让引用AspNetPager空间,也没法直接Using AspNetPager这样引用,不过也是画蛇添足啦,没什么大影响。

    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 WebApplication1
    {
        public partial class WebForm3 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if(!IsPostBack)
                {
                    GetDate();
                }
            }
    
            string ConStr = ConfigurationManager.ConnectionStrings["Connection"].ToString();
    
            public void GetDate()
            {
                //查出所有数据
                string GetDateSql = "SELECT * FROM [Info]";
                //计算一共有多少条数据,这个有待于接下来计算总页数
                string GetCountDataSql = "SELECT COUNT(*) FROM [Info]";
    
                using (SqlConnection conn = new SqlConnection(ConStr))
                {
                    conn.Open();
                    DataSet dt = new DataSet();
                    SqlDataAdapter sdt = new SqlDataAdapter(GetDateSql, conn);
                    //AspNetPager1.PageSize,一页显示多少数据,AspNetPager1.CurrentPageIndex:获取或设置当前显示页的索引。
                    //这一条语句对于dataset是一定要按照这个格式写的,具体的原理我也有点模糊,希望明白的朋友给出一些指点。
                    sdt.Fill(dt, (AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1)), AspNetPager1.PageSize,"MYTABLE");
                    this.Repeater1.DataSource = dt;
                    this.Repeater1.DataBind();
    
                    SqlCommand comm = new SqlCommand(GetCountDataSql, conn);
                    //AspNetPager1.RecordCount:获取或设置需要分页的所有记录的总数。
                    string GetCountData = comm.ExecuteScalar().ToString();
                    this.AspNetPager1.RecordCount =Convert.ToInt32(GetCountData);
                }
            }
    
            protected void AspNetPager1_PageChanged(object sender, EventArgs e)
            {
                GetDate();
            }
        }
    }
  • 相关阅读:
    android开发布局三(微信布局)
    Android开发adb,SQLite数据库运用
    直线电机磁负荷、电负荷
    MIT公开课(一):电场和偶极子
    哈工大电气工程系硕士研究生入学复试——自动控制原理1、2章
    直线电机与旋转电机的区别
    Math类介绍
    Scala编辑器和IntelliJ IDEA开发环境配置
    减少cpu的方法
    AS内存清理,建议以及查找内存泄露的方法
  • 原文地址:https://www.cnblogs.com/llcdbk/p/4021656.html
Copyright © 2011-2022 走看看