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来显示分页。
例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
//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; } } } shoesInfosList.Add(shoesInfo); } dr.Close(); return shoesInfosList; } } } 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; } } } } |