2011-11-02 27 views
0

我有現有的客戶端代碼,其中sql數據讀取器正在用於從數據庫中拉取記錄,在代碼後面,表格內容正在生成爲字符串,然後分配給一個asp:literal元素在頁面上。asp.net在動態生成的表中包括分頁

Dim myreader As SqlDataReader = selectcmd.ExecuteReader 

If myreader.HasRows Then 
str = str & "" // str is string 

While myreader.Read() 
If Not IsDBNull(myreader("column1")) Then 
    str = str & "<tr><td align=left><font color=white>" & myreader("column1") & "</font></td></tr>" 
End If 
End While 

// in the end string is assigned to Text attribute of asp:literal element 

客戶端希望使用分頁,並且一次只能看到10行,其餘應該通過分頁查看。

任何人都可以建議如何實現這一點。

謝謝。

回答

0

大部分時間在服務器端進行這種類型的HTML連接是一個非常糟糕的主意。

對於您的場景,您可以使用SQLDataSource將數據提供給gridview,並啓用對排序的分頁&的支持。除了在標記上聲明數據源之外,您不必編寫一行代碼就可以工作。

There's an excellent tutorial here.

如果不削減對你,你就必須落實要求數據庫準確返回你想要基於當前頁碼行數分頁自己。一般來說,您需要使用ROW_NUMBER()TOP來在數據庫端分頁結果。

Here's an example on how to do paging on the database side.

1

您是否嘗試過其中一種內置控件,如GridView?你可以用它來設置分頁,排序等。或者是有什麼理由將它寫入文字?