my favorite


GridView + CustomObjectDataSource 達到分頁(paging)的效果

由上一篇透過ObjectDataSource將MVC架構中的Model及View完全分離的方式,進而想去實作一個CustomObjectDataSource,解決在ObjectDataSource的實作中必須綁定特殊資料表的問題。而實作方式其實就是開發一個繼承ObjectDataSource的物件,並且擴充一個 SQL (string type)的屬性,在內部以SQL決定資料來源為何 (當然,考量到不同的資料庫時,也可以將Connection String包含進去),程式碼如下

namespace Huge.CustomControl {
[ToolboxData("<{0}:CustomObjectDataSource runat=server></{0}:CustomObjectDataSource>")]
public class CustomObjectDataSource : ObjectDataSource {
private string m_SQL = string.Empty;

public string SQL {
get {
String s = (String)ViewState["SQL"];
return ((s == null) ? string.Empty : s);

set {
ViewState["SQL"] = value;

/// <summary>
/// Initialize
/// </summary>
/// <param name="e"></param>
protected override void OnInit(EventArgs e) {
this.EnablePaging = true;
this.TypeName = "Huge.CustomControl.InnerImplementation";
this.SelectMethod = "GetDataView";
this.SelectCountMethod = "GetRowsCount";
this.StartRowIndexParameterName = "startRow";
this.MaximumRowsParameterName = "pageSize";

protected override void OnLoad(EventArgs e) {
this.SelectParameters.Add("sql", TypeCode.String, SQL);


namespace Huge.CustomControl {
/// <summary>
/// Inner implementation for CustomObjectDataSource
/// </summary>
public class InnerImplementation {
/// <summary>
/// Data Source
/// </summary>
private DataTable m_DataTable = null;

/// <summary>
/// 取得資料來源
/// </summary>
/// <param name="startRow">起始資料列項目</param>
/// <param name="pageSize">筆數</param>
/// <param name="sqlString">資料來源 SQL 字串</param>
/// <returns>DataView</returns>
public DataView GetDataView(int startRow, int pageSize, string sql) {
using (ExecutionEngine ee = ExecutionEngine.CreateNewExecutionEngine()) {
ExecutionQuery eq = new ExecutionQuery(sql, null);
this.m_DataTable = ee.ExecuteDataTable(eq);
DataTable PagedActivityTable = this.m_DataTable.Clone();
int size = startRow + pageSize;
for (int i = startRow; i < size && i < this.m_DataTable.Rows.Count; i++) {
/* 將取得的資料列匯入至PagedActivityTable */
return PagedActivityTable.DefaultView;
return null;

/// <summary>
/// 取得資料來源的總筆數
/// </summary>
/// <returns>資料來源總筆數</returns>
public int GetRowsCount(string sql) {
return this.m_DataTable.Rows.Count;

1 comment:

Anonymous said...


I am regular visitor of this website[url=].[/url]Plenty of useful information on Let me tell you one thing guys, some time we really forget to pay attention towards our health. In plain english I must warn you that, you are not serious about your health. Research displays that nearly 80% of all U.S. grownups are either fat or overweight[url=].[/url] Therefore if you're one of these citizens, you're not alone. Infact many among us need to lose 10 to 20 lbs once in a while to get sexy and perfect six pack abs. Now the question is how you are planning to have quick weight loss? [url=]Quick weight loss[/url] is not like piece of cake. If you improve some of your daily diet habbits then, its like piece of cake to quickly lose weight.

About me: I am author of [url=]Quick weight loss tips[/url]. I am also mentor who can help you lose weight quickly. If you do not want to go under hard training program than you may also try [url=]Acai Berry[/url] or [url=]Colon Cleansing[/url] for effortless weight loss.


Taipei, GuTing, Taiwan

huang47 | personal

huang47 | personal