zoukankan      html  css  js  c++  java
  • ASP.NET2.0 ObjectDataSource的使用详解(3)

    上次一个网友希望介绍一下自定义分页的问题,本文说明如何使用ObjectDataSource自定义分页、排序,你会发现ObjectDataSource的伸缩性很大,。不管是初学者还是具有一定经验的用户,ObjectDataSource总能够给你提供能够满足你要求的功能。

        在数据分页中,最简单是利用GridView的分页、排序功能,此功能不几乎应该是确实不需要编写代码,稍微勾勾划划就能够分页、排序。然而当数据量很少时,以来此方法确实可以减轻程序员的负担,但是当数据很多,例如记录几十万、上百万时,使用系统自带的分页将导致大量数据回复,因此使用自定义分页就显得更为有效。

    概括起来,天天总结自定义数据分页主要包含四种方式:
    1) 使用临时表――此方法被广泛使用论坛CommunityServer、博客等开源代码
    2) 使用存储过程――这个方法好像最初来自CSDN上的一篇,可以到如下网址查看博客圆里的一篇文章
    http://genson.cnblogs.com/archive/2006/01/17/318882.html
    3) 利用SQL语句选取有限数据分页,此方法我用过,感觉有一些问题,还有待进一步证实。
    4) 可以利用GirdView的客户端到服务器的回调获得新页的数据,这是ASP.NET2.0新增的一个功能。

    本文主要介绍第一种使用临时表进行分页。以后会介绍其它方式

    下面是对上面文章的更改。代码如下,使用临时表进行自定义分页:

    public List<Product> LoadAllProduct(int startIndex, int maxRows, string sortedBy)

        {

            List<Product> products = new List<Product>();

                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

               

        string commandText = @"

         -- 为分页建立一张临时表

        CREATE TABLE #TempPageTable

        (

            IndexId int IDENTITY (0, 1) NOT NULL,

             id int   

         )

        -- 读取数据插入临时表

        INSERT INTO #TempPageTable

         (

             [id]

         )

         SELECT

             [Productid]   

         FROM Products";

             if (sortedBy != "")

            {

                  commandText += " ORDER BY " + sortedBy;

             }

             commandText += @"

        SET @totalRecords = @@ROWCOUNT

          

        SELECT

             src.[ProductID],

             src.[ProductName],

             src.[CategoryID],

             src.[Price],

            src.[InStore],

            src.[Description]  

         FROM Products src, #TempPageTable p

         WHERE 

             src.[productid] = p.[id] AND

            p.IndexId >= @StartIndex AND p.IndexId < (@startIndex + @maxRows)";

            

            if (sortedBy != "") {

                  commandText += " ORDER BY " + sortedBy;

             }

             SqlCommand command = new SqlCommand(commandText, conn);

             command.Parameters.Add(new SqlParameter("@startIndex", startIndex));

             command.Parameters.Add(new SqlParameter("@maxRows", maxRows));

             command.Parameters.Add(new SqlParameter("@totalRecords", SqlDbType.Int));

             command.Parameters["@totalRecords"].Direction = ParameterDirection.Output;

             conn.Open();

             SqlDataReader dr = command.ExecuteReader();

             while (dr.Read()) {

                  Product prod = new Product();

                  prod.ProductID = (int)dr["ProductID"];

                  prod.ProductName= (string)dr["ProductName"];

                prod.CategoryID = (int)dr["CategoryID"];

                  prod.Price = (decimal)dr["price"];

                prod.InStore=(Int16)dr["InStore"];

                prod.Description=(String)dr["Description"];

                  products.Add(prod);

             }

             dr.Close();

             conn.Close();

             _count = (int)command.Parameters["@totalRecords"].Value;

             return products;

         }

        public int CountAll()

        {

            return _count;

        }


    简单解释如下:
    1)这里定义了一个临时表 #TempPageTable,临时表的定义需要加“#”,临时表的好处是自动创建,并在不需要时候进行销毁。具体介绍请参考SQL的帮助系统。
    2)在临时表里我建立了一个索引印列IndexId和id列。如果你查看我的数据库Producst表的设计可以看到该表包含一个ProductID列,该列是一个自增型标识种子,那么为什么还需要建立IndexId列?
    这是因为Product表的ProductID列是一个自增形式,所以序号将会在你编辑时可能会打乱,例如原来产品记录是1,2,3,4,5后来你删除了一条记录,例如5,那么当你再增加一条记录时,新的序列号将是从6开始,而不会使用原来的5。
    为了让索引不断号的自增,使用了自定义了自增的IndexId临时表。
    3)临时表里的id列对应ProductID,正如你看到的,该id列插入的数据实际上来自Products表的ProductID列

       
    下面是在页面使用的源代码:

    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="LoadAllProduct" TypeName="ProductBLL" DataObjectTypeName="Product"

            EnablePaging="True" MaximumRowsParameterName="maxRows" StartRowIndexParameterName="startIndex" SelectCountMethod="CountAll" SortParameterName="sortedBy"

            ></asp:ObjectDataSource>

            &nbsp;&nbsp;<asp:GridView ID="GridView1" runat="server" CellPadding="4" Font-Names="Verdana"

                Font-Size="X-Small" ForeColor="#333333" GridLines="None" DataSourceID="ObjectDataSource1" AllowPaging="True" AllowSorting="True">

                <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />

    具体的解释请自己研究吧。

    单击此处源代码下载/Files/mqingqing123/ObjectDataSourceExample.rar


    使用VS.NET2005或者VWD2005以“File System”放置打开,可以直接运行本文源代码。



    上面给出的是使用临时表进行分页,除此以外,GridView还支持回发分页。
     GridView 和 DetailsView 支持一种特殊的分页和排序模式,它利用客户端到服务器的回调获得新页的数据或新排序的数据。若要启用此功能,请将 EnableSortingAndPagingCallbacks 属性设置为 true。注意,执行分页或排序操作时,不需要回发该页就能检索新值(不过进行了到服务器的客户端脚本回调)。当 GridView 或 DetailsView 包含模板化字段时,此功能不受支持。当启用了此功能时,也不支持在 CommandField 中显示“Select”(选择)按钮。
    下面是一个使用客户回发分页的例子,来自ASP.NET快速入门,注意需要将GridView的EnableSortingAndPagingCallbacks设置为="True"


    代码如下
    <%@ Page Language="C#" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Paging and Sorting Using Callbacks</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
          <asp:GridView AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"
            DataKeyNames="au_id" DataSourceID="SqlDataSource1" EnableSortingAndPagingCallbacks="True"
            ID="GridView1" runat="server">
            <Columns>
              <asp:BoundField DataField="au_id" HeaderText="au_id" ReadOnly="True" SortExpression="au_id" />
              <asp:BoundField DataField="au_lname" HeaderText="au_lname" SortExpression="au_lname" />
              <asp:BoundField DataField="au_fname" HeaderText="au_fname" SortExpression="au_fname" />
              <asp:BoundField DataField="state" HeaderText="state" SortExpression="state" />
            </Columns>
          </asp:GridView>
          <asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:Pubs %>" ID="SqlDataSource1"
            runat="server" SelectCommand="SELECT [au_id], [au_lname], [au_fname], [state] FROM [authors]">
          </asp:SqlDataSource>
       
        </div>
        </form>
    </body>
    </html>

  • 相关阅读:
    Extjs Ext.ux.IFrame的用法 以及父子窗口间函数相互调用
    Android ADB server didn't ACK * failed to start daemon * 简单有效的解决方案
    Java sun.misc.Unsafe类的学习笔记
    Java 并发编程学习笔记 理解CLH队列锁算法
    深入理解Java虚拟机 -- 读书笔记(1):JVM运行时数据区域
    Java并发编程学习笔记 深入理解volatile关键字的作用
    JVM Client Server启动设置
    双重检查锁定与延迟初始化
    Tomcat 添加为系统服务 开机自动启动
    配置TOMCAT 修改默认ROOT路径
  • 原文地址:https://www.cnblogs.com/yangjunwl/p/1029747.html
Copyright © 2011-2022 走看看