Inside ASP.NET 2.0 DataBound Control – 1

來源:互聯網
上載者:User

原創文章,如需轉載,請註明出處。

ASP.NET 2.0 較之ASP.NET
1.0和1.1提供了新的資料繫結模型,添加了一系列新的資料展現、編輯控制項譬如GridView、DetailView以及FormsView等控制項。

本文旨在闡述ASP.NET 2.0新的資料繫結控制項架構,並且逐一介紹每一個控制項。

1. ASP.NET 2.0 DataBound Control結構

2. BaseDataBoundControl

從我們可以看到BaseDataBoundControl是所有資料繫結控制項的基類。

該類定義了基本的資料繫結功能,能夠用來展示和操作ASP.NET
2.0新的資料來源控制項,同時也繼續支援原來ASP.NET 1.1的資料繫結方式。

看一下BaseDataBoundControl的主要介面:

 1public class BaseDataBoundControl
 2{
 3     public virtual object DataSource  { get; set; } 
 4     public virtual string DataSourceId { get; set; } 
 5
 6     public override void DataBind(); 
 7     protected abstract void PerformSelect(); 
 8
 9     protected abstract void ValidateDataSource(object dataSource); 
10
11     public event EventHandler DataBound {add; remove; } 
12}

DataSource屬性用來支援ASP.NET 1.1的資料繫結方式,當設定了DataSource屬性後,控制項將會調用ValidateDataSource方法來進行驗證,通常情況下設定的對象應該是IListSource、IEnumerable或者IDataSource。

而DataSourceID屬性則指定了資料來源控制項的ID,資料繫結控制項將會從該資料來源控制項中擷取資料。

BaseDataBoundControl定義了一個抽象方法PerformSelect用來從繫結資料源中擷取資料,然後重載DataBind方法來調用PerformSelect。所有從BaseDataBoundControl派生的類都應當重載PerformSelect方法來實現資料擷取,而不應當重載DataBind。
參見DataBind方法的實現: 1public override void DataBind()
 2{
 3    if (base.DesignMode)
 4    {
 5       
 6    }
 7    this.PerformSelect();
 8}

值得一提的是BaseDataBoundControl在頁面生命週期中定義了一些時機來進行資料繫結:

  • OnInit
  • 如果控制項的ViewState被禁掉的話,則要求進行資料繫結。

  • OnPagePreLoad
  • 設定了一個內部標誌表明控制項初始化已經完成。

  • OnPreRender
  • 設定了一個內部標誌表示控制項當前生命週期狀態,然後嘗試進行資料繫結。

從BaseDataBoundContro派生出了兩個基本的資料繫結控制項:DataBoundControl 和HierarchicalDataBoundControl,前者用於綁定List或者Table狀的資料,而後者用於綁定樹狀資料。我們也可以看到ListBox, DropDownList,CheckboxList還有GridView都從DataBoundControl 派生,而TreeView以及Menu則從HierarchicalDataBoundControl派生。

下一篇文章將會講述DataBoundControl.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.