GirdView自訂分頁

來源:互聯網
上載者:User

本文說明如何使用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" />

具體的解釋請自己研究吧。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.