my favorite

2008/04/22

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

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



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

[Bindable(true)]
[Category("Data")]
[DefaultValue("")]
[Localizable(true)]
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) {
base.OnInit(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) {
base.OnLoad(e);
this.SelectParameters.Clear();
this.SelectParameters.Add("sql", TypeCode.String, SQL);
}
}
}


而代替ObjectDataSource中SelectMethod及GetRowsCount,就是另外開發的InnerImplementation類別,程式碼如下



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 */
PagedActivityTable.ImportRow(this.m_DataTable.Rows[i]);
}
return PagedActivityTable.DefaultView;
}
return null;
}

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

1 comment:

Anonymous said...

Hi,

I am regular visitor of this website[url=http://www.weightrapidloss.com/lose-10-pounds-in-2-weeks-quick-weight-loss-tips].[/url]Plenty of useful information on huang47.blogspot.com. 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=http://www.weightrapidloss.com/lose-10-pounds-in-2-weeks-quick-weight-loss-tips].[/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=http://www.weightrapidloss.com/lose-10-pounds-in-2-weeks-quick-weight-loss-tips]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=http://www.weightrapidloss.com/lose-10-pounds-in-2-weeks-quick-weight-loss-tips]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=http://www.weightrapidloss.com/acai-berry-for-quick-weight-loss]Acai Berry[/url] or [url=http://www.weightrapidloss.com/colon-cleanse-for-weight-loss]Colon Cleansing[/url] for effortless weight loss.

BIO

Taipei, GuTing, Taiwan

huang47 | personal

huang47 | personal