业务逻辑>数据层>ObjectDataSource>数据源分页机制

来源于大师讲堂 ASP.NET 3.5 开发范例精讲精析

总体思路跟select id from student where id>1 and id<10; 差不多。

就是,数据表的记录总数,指定开始的记录,然后读取的条数。

具体步骤:

(1)将 ObjectDataSource 控件的 EnablePaging 属性设置为 True;
(2)一旦 ObjectDataSource 控件的 EnablePaging 属性设置为 True,ObjectDataSource 控件就会传递两个名称分别为 starRowIndex 和 maximumRows 的参数给 SelectMethod 属性所指定的方法,也就是说,业务对象用来读取数据的方法必须接收这两个参数,以便通过它们来读取特定分页的记录。
(3)另外,还必须为业务对象编写一个方法,用来返回数据表的记录总数,并将此方法的名称赋予给 ObjectDataSource 控件的 SelectCountMethod 属性;
(4)同时,GridView, DetailsView 等等数据绑定控件的分页功能也需要启用,也就是将这些控件的 AllowPaging 属性设置为 True。

前台代码:

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Demo10.aspx.cs" Inherits="Demo10" %>
 2 
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head runat="server">
 7     <title>ObjectDataSource 控件 + 数据源分页机制</title>
 8 </head>
 9 <body>
10     <form id="form1" runat="server">
11      <style type="text/css">
12         #form1
13         {
14             text-align: center;
15         }
16         body
17         {
18             font-family: Lucida Sans Unicode;
19             font-size: 10pt;
20         }
21         button
22         {
23             font-family: tahoma;
24             font-size: 8pt;
25         }
26         .highlight
27         {
28             display: block;
29             color: red;
30             font: bold 24px Arial;
31             margin: 10px;
32         }
33     </style>
34     
35     <div>
36     <h2>
37             ObjectDataSource 控件 + 数据源分页机制</h2>
38         <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
39                  DataSourceID="ObjectDataSource1" PageSize="5" EnableViewState="False" 
40                  HorizontalAlign="Center" >
41         </asp:GridView>
42         <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" EnablePaging="True" 
43                  SelectCountMethod="GetLimingchStudioCount" SelectMethod="GetLimingchMember" 
44                  TypeName="Demo10"></asp:ObjectDataSource>
45     </div>
46     </form>
47 </body>
48 </html>
49 

TypeName="Demo10"对应的类:

  1 using System;
  2 using System.Data;
  3 using System.Configuration;
  4 using System.Linq;
  5 using System.Web;
  6 using System.Web.Security;
  7 using System.Web.UI;
  8 using System.Web.UI.HtmlControls;
  9 using System.Web.UI.WebControls;
 10 using System.Web.UI.WebControls.WebParts;
 11 using System.Xml.Linq;
 12 using System.Data.SqlClient;
 13 using System.Web.Configuration;
 14 
 15 /// <summary>
 16 ///Demo10 的摘要说明
 17 /// </summary>
 18 public class Demo10
 19 {
 20     private string _connectionString;
 21 
 22 
 23     public Demo10()
 24     {
 25         //
 26         //TODO: 在此处添加构造函数逻辑
 27         //
 28 
 29         Initialize();
 30     }
 31 
 32     //获取连接字符串
 33     public void Initialize()
 34     {
 35         // 初始化数据源。我们使用 Web.config 中名称为 chtNorthwind 的连接字符串。
 36 
 37         if (ConfigurationManager.ConnectionStrings["chtNorthwind"== null || ConfigurationManager.ConnectionStrings["chtNorthwind"].ConnectionString.Trim() == "")
 38         {
 39             throw new Exception("名称为 'chtNorthwind' 的连接字符串务必内含于 " + " <connectionStrings> 配置设置区段中。");
 40         }
 41 
 42         // 将连接字符串的内容储存于变量 _connectionString 中。
 43         _connectionString = ConfigurationManager.ConnectionStrings["chtNorthwind"].ConnectionString;
 44     }
 45 
 46     //通过参数startRowIndex,maximumRows传入,得到数据表的部分行数,对应ObjectDataSource1属性SelectMethod
 47     public DataTable GetLimingchMember(int startRowIndex, int maximumRows)//两个参数,对应ObjectDataSource1属性,StartRowIndexParameterName,MaximumRowsParameterName,指定读取数据的范围
 48     {
 49 
 50         string commandText = "SELECT 员工号码,姓名,性别,地址,部门 FROM 章立民研究室";
 51 
 52         // 建立一个数据配接器对象。
 53         SqlDataAdapter da = new SqlDataAdapter(commandText, _connectionString);
 54 
 55         // 建立一个 DataSet 对象。
 56         DataSet ds = new DataSet();
 57 
 58         using (da)
 59         {
 60 
 61             // 从 startRowIndex 参数所指定的数据行开始,提取 maximumRows 参数所指定的笔数,
 62             // 然后将它们填入 DataSet 对象中的「章立民研究室」数据表。
 63             da.Fill(ds, startRowIndex, maximumRows, "章立民研究室");
 64 
 65         }
 66 
 67         // 传回 DataTable 物件。
 68         if (ds.Tables["章立民研究室"!= nullreturn ds.Tables["章立民研究室"];
 69 
 70         return null;
 71 
 72     }
 73 
 74     //通过HttpContext返回数据表的行数,便于ObjectDataSouce使用,对应ObjectDataSource1属性SelectCountMethod
 75     public int GetLimingchStudioCount()
 76     {
 77         HttpContext context = HttpContext.Current;
 78         if (context.Cache["LimingchStudioCount"== null)
 79         {
 80             context.Cache["LimingchStudioCount"= GetLimingchStudioCountFromSqlDB();
 81         }
 82         return (int)context.Cache["LimingchStudioCount"];
 83     }
 84 
 85 
 86     //得到数据表的行数
 87     private int GetLimingchStudioCountFromSqlDB()
 88     {
 89         int nRows = 0;
 90 
 91         // 建立一个连接对象。
 92         SqlConnection con = new SqlConnection(_connectionString);
 93 
 94         // 建立一个数据命令对象。
 95         SqlCommand cmd = new SqlCommand();
 96         cmd.Connection = con;
 97         cmd.CommandText = "SELECT Count(*) FROM 章立民研究室";
 98 
 99         // 运行命令。
100         using (con)
101         {
102             con.Open();
103             nRows = (int)cmd.ExecuteScalar();
104         }
105         return nRows;
106     }
107 
108 
109 }
110 
就是这样。

合乎自然而生生不息。。。
原文地址:https://www.cnblogs.com/samwu/p/1788293.html