您的位置:首页 > 移动开发 > Objective-C

一步一步学习ObjectDataSource控件--自定义分页排序

2007-01-16 09:38 585 查看
一步一步学习ObjectDataSource控件--自定义分页排序
在上篇http://mqingqing123.cnblogs.com/archive/2006/04/07/369020.html
介绍了ObjectDataSource的常规使用。

上次一个网友希望介绍一下自定义分页的问题,本文说明如何使用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>

  <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”放置打开,可以直接运行本文源代码。

posted on 2006-04-10 11:06 天天 阅读(1668) 评论(7) 编辑 收藏 引用 网摘 所属分类: ASP.NET V2.0



//Sys.WebForms.PageRequestManager._initialize('AjaxHolder$scriptmanager1', document.getElementById('Form1'));
Sys.WebForms.PageRequestManager.getInstance()._updateControls(['tAjaxHolder$UpdatePanel1'], [], [], 90);

//]]>

FeedBack:

# re: 一步一步学习ObjectDataSource控件--自定义分页排序
2006-04-12 16:23 | WOW玩家

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

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

></asp:ObjectDataSource>

能解释一下ObjectDataSource上面列出来的属性的大概意思吗?? 回复 更多评论


# re: 一步一步学习ObjectDataSource控件--自定义分页排序
2006-04-14 14:13 | 人生!

可以参考这一篇文章
http://rhs.cnblogs.com/articles/329648.html 回复 更多评论


# re: 一步一步学习ObjectDataSource控件--自定义分页排序
2006-04-19 12:11 | ysp

能说明一下这两个参数是如何传递的吗?MaximumRowsParameterName="maxRows" StartRowIndexParameterName="startIndex" 回复 更多评论


# re: 一步一步学习ObjectDataSource控件--自定义分页排序
2006-04-19 13:58 | 天天

在使用ObjectDataSource进行自定义分页时,除了需要将GridView的AllowPaging="True" 外,还需要将ObjectDataSource设置为“ EnablePaging="True"”,在此处情况下,ObjectDataSource将不再自动维护分页信息,所以我们就需要自己来设置分页处理。
自定义分页主要考虑三个问题:
1)每页有多少条记录
2)共有多少条记录
3)当前页是第几页
这样就需要三个参数处理,但是第一个已经由GridView完成了,默认的GridView的PageSite为10,也就是每页默认有10条记录。
而后买两个功能就需要ObjectDataSource完成
1)SelectCountMethod,正如其名,获取数据库里有多少条记录,当然可以简单的认为该数字为select count(*) from database
2)StartRowIndexParameterName设置当前是第几页
上面两个功能其实和DataGrid的自定义分页一样,我在《ASP.NET技术详解与应用实例》里曾经详细的说过
接下来就是新的了,参数处理
既然参数的功能已经知道了,那么如何传递给函数LoadAllProduct,您可以看到本例子里LoadAllProduct的声明为:
public List<Product> LoadAllProduct(int startIndex, int maxRows, string sortedBy)
这里有两个参数:startIndex和maxRows,它们就是分别设定当前是第几页的,和总记录数
(另外一个是排序)
这些参数的名称比较重要,您可能想设置LoadAllProduct为
public List<Product> LoadAllProduct(int start, int max, string sortedBy)
这样将发生错误,因为ObjectDataSource采用的是参数是以声明的方式定义,所以这里的参数必须和StartRowIndexParameterName,MaximumRowsParameterName指定的值一致
(参数的顺序无所谓) 回复 更多评论


# re: 一步一步学习ObjectDataSource控件--自定义分页排序
2006-05-20 21:32 | MagicBeans

这个代码使用的分页属于你所介绍的种类吗?

public static DataTable Query(string SQLString, int startRecord, int maxRecords)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataTable dt = new DataTable();
try
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
command.Fill(startRecord, maxRecords, dt);
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
return dt;
}
} 回复 更多评论


# re: 一步一步学习ObjectDataSource控件--自定义分页排序
2006-08-04 10:05 | 凡人

使用ObjectDataSource控件时遇到一个问题:无法获得返回的值。
例如:当选择数据是需要返回总页数这个参数,把Direction设置为"Output",但怎么获得totalPages这个参数的值呢?找了很多资料都没介绍。
<SelectParameters>
<asp:Parameter Name="totalPages" Type="Int32" Direction="Output" />
</SelectParameters>
如果把返回值指向一个文本框也不行("totalPages"为文本框ID) ,代码如下:
<SelectParameters>
<asp:ControlParameter Name="totalPages" Type="Int32" ControlID="totalPages" Direction="Output" PropertyName="Text"></asp:ControlParameter>
</SelectParameters>
请指教,谢谢! 回复 更多评论
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: