内容详情 您现在的位置是: 首页> 其他随笔

关于PagedDataSource分页属性和简单例子演示(数据库可自行准备简单数据进行测试)<原创内容>

发布时间:2022-04-03 19:20 已围观:1485

摘要关于PagedDataSource分页属性和简单例子演示(数据库可自行准备简单数据进行测试)<原创内容>

PagedDataSource 类封装 DataGrid 控件的属性,这些属性使 DataGrid 可以执行分页。
PagedDataSource 类的部分公共属性:

 AllowCustomPaging  获取或设置指示是否启用自定义分页的值。 
 AllowPaging   获取或设置指示是否启用分页的值。 
 Count    获取要从数据源使用的项数。 
 CurrentPageIndex   获取或设置当前页的索引。 
 DataSource   获取或设置数据源。 
 DataSourceCount   获取数据源中的项数。 
 FirstIndexInPage   获取页中的第一个索引。 
 IsCustomPagingEnabled  获取一个值,该值指示是否启用自定义分页。 
 IsFirstPage   获取一个值,该值指示当前页是否是首页。 
 IsLastPage   获取一个值,该值指示当前页是否是最后一页。 
 IsPagingEnabled   获取一个值,该值指示是否启用分页。 
 IsReadOnly   获取一个值,该值指示数据源是否是只读的。 
 IsSynchronized   获取一个值,该值指示是否同步对数据源的访问(线程安全)。 
 PageCount   获取显示数据源中的所有项所需要的总页数。 
 PageSize   获取或设置要在单页上显示的项数。 
 VirtualCount   获取或设置在使用自定义分页时数据源中的实际项数。 

DataGrid控件就是使用PagedDataSource类来实现数据分页显示的,所以DataList和Repeater也同样可以使用PagedDataSource来显示分页。

例子


//Model层
using
System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Model {   public class ShoesInfo    {public ShoesInfo()        {        }public ShoesInfo(int shoesId, string shoesName, decimal shoesPrice, decimal shoesDelPrice, string shoesFartherImgUrl, string shoesFirstSonImgUrl, string shoesSecondSonImgUrl, string shoesthirdSonImgUrl, string shoesfourthSonImgUrl, string shoesShortDec, int stock, int monthlySales, int brandId)        {this.shoesId = shoesId;this.shoesName = shoesName;this.shoesPrice = shoesPrice;this.shoesDelPrice = shoesDelPrice;this.shoesFartherImgUrl = shoesFartherImgUrl;this.shoesFirstSonImgUrl = shoesFirstSonImgUrl;this.shoesSecondSonImgUrl = shoesSecondSonImgUrl;this.shoesthirdSonImgUrl = shoesthirdSonImgUrl;this.shoesfourthSonImgUrl = shoesfourthSonImgUrl;this.shoesShortDec = shoesShortDec;this.stock = stock;this.monthlySales = monthlySales;this.brandId = brandId;        }public int shoesId { get; set; }public string shoesName { get; set; }public decimal shoesPrice { get; set; }public decimal shoesDelPrice { get; set; }public string shoesFartherImgUrl { get; set; }public string shoesFirstSonImgUrl { get; set; }public string shoesSecondSonImgUrl { get; set; }public string shoesthirdSonImgUrl { get; set; }public string shoesfourthSonImgUrl { get; set; }public string shoesShortDec { get; set; }public int stock { get; set; }public int monthlySales { get; set; }public int brandId { get; set; }    } }





//DAL数据访问层
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
namespace DAL
{
   public class ShoesInfoService
    {
     /// <summary>
        /// 查询信息
        /// </summary>
        /// <returns>list</returns>
        public static List<ShoesInfo> SelectShoesInfoByPage()
        {
            //泛型
            List<ShoesInfo> shoesInfosList = new List<ShoesInfo>();
            string sql = "select * from ShoesInfo";
            SqlDataReader dr = DBHelper.GetDataReader(sql);
            while (dr.Read())
            {
                ShoesInfo shoesInfo = new ShoesInfo();
                shoesInfo.shoesId = dr.GetInt32(0);
                shoesInfo.shoesName = dr.GetString(1);
                shoesInfo.shoesPrice = dr.GetDecimal(2);
                shoesInfo.shoesDelPrice = dr.GetDecimal(3);
                shoesInfo.shoesFartherImgUrl = dr.GetString(4);
                shoesInfo.shoesFirstSonImgUrl = dr.GetString(5);
                shoesInfo.shoesSecondSonImgUrl = dr.GetString(6);
                shoesInfo.shoesthirdSonImgUrl = dr.GetString(7);
                shoesInfo.shoesfourthSonImgUrl = dr.GetString(8);
                shoesInfo.shoesShortDec = dr.GetString(9);
                shoesInfo.stock = dr.GetInt32(10);
                shoesInfo.monthlySales = dr.GetInt32(11);
                shoesInfo.brandId = dr.GetInt32(12);

                shoesInfosList.Add(shoesInfo);
            }

            dr.Close();
            return shoesInfosList;
        }
    }
}


//DBHelper类

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class DBHelper
    {
        //连接字符串
        private static string connStr = "Data Source=.;Initial Catalog=TideshoeDB;Integrated Security=True";

        //数据库连接对象
        public static SqlConnection conn = null;

        /// <summary>
        /// 建立数据连接方法,初始化数据库连接对象
        /// </summary>
        public static void InitConnection()
        {
            if (conn == null)
            {
                conn = new SqlConnection(connStr);
                conn.Open();
            }else if (conn.State == ConnectionState.Broken)
            {
                conn.Close();
                conn.Open();
            }else if(conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
        }


        /// <summary>
        /// SELECT执行查询
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <returns>非断开式,用于检索数据库的读取器SqlDataReader的对象</returns>
        public static SqlDataReader GetDataReader(string sql)
        {
            //创建连接对象
            InitConnection();

            //cmd命令
            SqlCommand cmd = new SqlCommand(sql, conn);
            return cmd.ExecuteReader();
        }




//BBL业务逻辑层
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL;
using Model;

namespace BBL
{
   public class ShoesInfoManager
    {
        /// <summary>
        /// 查询鞋子信息前minId-maxId条:比例 显示前9-16条
        /// p:“至”的意思
        /// </summary>
        /// <returns>dataTable</returns>
        public static DataTable SelectTopminIdpmaxIdShoesInfo(int minId, int maxId)
        {
            return ShoesInfoService.SelectTopminIdpmaxIdShoesInfo(minId, maxId);
        }

        public static List<ShoesInfo> SelectShoesInfoByPage()
        {
            return ShoesInfoService.SelectShoesInfoByPage();
        }
    }
}


//Web视图层
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="loginText.aspx.cs" Inherits="WebSurface.loginText" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Repeater ID="ShoesInfoByShowRepeater" runat="server">
           <ItemTemplate>
         <img class="pri-img" src='./Images/ShoesInfoImg/<%# Eval("shoesFartherImgUrl") %>' alt="product">//显示图片
       </ItemTemplate>
        </asp:Repeater>
     //上下页
    <div class="paginatoin-area text-center">
            <ul class="pagination-box">
                <li><asp:HyperLink ID="HyperLinkByPre" runat="server" CssClass="previous" Visible="True"><i class="lnr lnr-chevron-left"></i>上一页</asp:HyperLink></li>
                <li><asp:HyperLink ID="HyperLinkByNext" runat="server" CssClass="next"><i class="lnr lnr-chevron-right"></i>下一页</asp:HyperLink></li>
            </ul>
       </div>
    </form>
</body>
</html>




//web视图逻辑代码层
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BBL;
namespace WebSurface
{
    public partial class NotLogShop : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack){
                //    ShoesInfoByShowRepeater.DataSource = ShoesInfoManager.SelectTopminIdpmaxIdShoesInfo(0, 10);
                //    ShoesInfoByShowRepeater.DataBind();
                BindShoesPageData();
            }
           
        }

        public void BindShoesPageData()
        {
            //创建分页对象
            PagedDataSource pagedData = new PagedDataSource();
            //绑定数据
            pagedData.DataSource = ShoesInfoManager.SelectShoesInfoByPage();
        //开启分页
            pagedData.AllowPaging = true;
       //一页数据显示数量
            pagedData.PageSize = 9;
       //当前页
            int currentIndex = Request.QueryString["pageIndex"] == null ? 0 : int.Parse(Request.QueryString["pageIndex"]);
            pagedData.CurrentPageIndex = currentIndex;
       //给Repeater绑定数据源pagedData(分页对象
            ShoesInfoByShowRepeater.DataSource = pagedData;
            ShoesInfoByShowRepeater.DataBind();

            if (currentIndex == 0 || Request.QueryString["pageIndex"] == null)
            {
                HyperLinkByPre.Enabled = false;
            }
            else
            {
                HyperLinkByPre.NavigateUrl = "NotLogShop.aspx?pageIndex=" + (pagedData.IsFirstPage ? 0 : currentIndex - 1);
                HyperLinkByPre.Enabled = true;
            }
            bool isLs = pagedData.IsLastPage;//如果是最后一页返回true;否则返回false
            if (pagedData.IsLastPage)
            {
                HyperLinkByNext.Enabled = false;
            }
            else
            {
                HyperLinkByNext.NavigateUrl = "NotLogShop.aspx?pageIndex=" + (pagedData.IsLastPage ? currentIndex : currentIndex + 1);
                HyperLinkByNext.Enabled = true;
            }
        }


    }
}


初写 如果大家发现内容有错误的地方可联系我及时改正 麻烦啦!


易,~


点个赞关个注再走呗~


作者:黎哈哈



 

声明:本文内容摘自网络,版权归原作者所有。如有侵权,请联系处理,谢谢~
转发:黎三岁--https://www.cnblogs.com/lmbl/p/16002178.html

赞一个 (79)