1. 程式人生 > >一步一步學習ObjectDataSource控制元件--自定義分頁排序

一步一步學習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>

&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”放置開啟,可以直接執行本文原始碼。