Inside ASP.NET 2.0 DataBound Control - 2

l原创文章,如需转载,请注明出处。

DataBoundControl

žDataBoundControl是一个抽象类,从BaseDataBound类派生,用来绑定List或者Table状数据。所有用来显示或编辑List或Table状数据的绑定控件都应该从该类派生,例如ListBox,DropDownList,CheckboxList还有GridView。

DataBoundControl实现的主要功能是从数据源中获取数据,无论用户是通过设置DataSource还是通过DataSourceID进行的数据绑定。获取到的数据将以IEnumerable的方式提供给派生类,这样派生类将不再需要关心如何从数据源中获取数据,而只需要关心其本身的业务逻辑即如何展示绑定数据。

1.  DataBoundControl接口:
 1public abstract class DataBoundControl : BaseDataBoundControl
 2{
 3    public virtual string DataMember getset; }
 4    protected DataSourceSelectArguments SelectArguments get; }
 5
 6    protected override void PerformSelect();
 7    protected virtual void PerformDataBinding( IEnumerable data);
 8
 9    protected virtual DataSourceView GetData();
10    protected virtual IDataSource GetDataSource();
11}

    DataMember
DataMember属性用来指定绑定数据列表名称,用户设置的数据源可能会包含多个不同的数据项列表。比如绑定到控件的数据源控件含有多个DataSourceView,或者绑定到控件的数据源是一个DataSet,而这个DataSet含有多个数据表格。

    SelectArguments
而SelectArguments属性则定义了用于向数据源控件检索数据时使用的 DataSourceSelectArgument 对象,注意使数据源控件,也就是通过DataSourceID进行的数据绑定。

通过该对象可以指定检索数据的起始行位置、最大行数,设定排序表达式,同时还可以检索数据源的总行数。

下面是DataSourceSelectArgument 对象的接口:
 1public sealed class DataSourceSelectArguments
 2{    
 3    public int StartRowIndex getset; }
 4    public int MaximumRows getset; }
 5
 6    public string SortExpression getset; }
 7
 8    public bool RetrieveTotalRowCount getset; }
 9    public int TotalRowCount getset; }
10}

DataBoundControl的派生类可以通过配置SelectArguments来优化数据查询,比如当控件支持分页功能的时候,可以只检索当前页面所需数据。

    PorformSelect, PerformDataBinding
DataBoundControl重载了PerformSelect方法,在这个方法中控件将会向数据源检索数据,并且将检索到的数据作为参数来调用PerformDataBinding方法。

PerformDataBinding是DataBoundControl类定义的虚方法,派生类只需要Override该方法,处理检索到的数据就可以了。

    GetDataSource
从绑定的数据源中获取相关联的IDataSource对象,用户必须是通过设置DataSourceID属性来进行数据绑定的,否则将会返回一个空引用。

看一下IDataSource这个接口:
1public interface IDataSource
2{
3    event EventHandler DataSourceChanged;
4
5    DataSourceView GetView(string viewName);
6    ICollection GetViewNames();
7}


可以看到得到IDataSource对象之后,就可以获得当前绑定的DataSoutceView了。另外当DataBoundControl捕获到IDataSource对象的DataSourceChanged事件后,会主动进行重新绑定。

   GetData
从绑定的数据源中获取用于执行数据操作的 DataSourceView对象,通过该对象数据绑定控件可以对数据源进行各种操作,比如Select, Insert还有Update。

2.  页面生命周期
DataBoundControl在页面生命周期中定义了一些时机来进行数据绑定的处理:

  OnPagePreLoad
如果是页面进行第一次请求的话,则控件要求进行数据绑定。另外如果是一次PostBack的话,并且控件仍然没有进行数据绑定,同时Enable ViewState,控件也会要求数据绑定。
见下面的代码: 
 1protected override void OnPagePreLoad(object sender, EventArgs e)
 2{
 3    base.OnPagePreLoad(sender, e);
 4    if (this.Page != null)
 5    {
 6        if (!this.Page.IsPostBack)
 7        {
 8            base.RequiresDataBinding = true;
 9        }

10        else if (base.IsViewStateEnabled && (this.ViewState["_!DataBound"== null))
11        {
12            base.RequiresDataBinding = true;
13        }

14    }

15    this._pagePreLoadFired = true;
16}
这就是为什么在设计时只设置数据绑定控件的DataSourceID,在运行时控件会自动进行数据绑定。

   OnLoad
如果说在OnPreLoad阶段没有进行数据绑定的话,在OnLoad阶段控件还会进行检查,并且尝试进行数据绑定如果条件合适的话。

3。小结
DataBoundControl实现了数据绑定控件的基本功能,例如从数据源中获取数据,以及如何操作数据源。其派生类所作的应该是如何展现数据,以及如何利用其提供的接口来操作数据源。

相关示例大家可以参考MSDN文档:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.databoundcontrol.aspx

下一节将会讲述HierarchicalDataBoundControl控件。

posted @ 2008-06-16 21:19  ted  阅读(1996)  评论(2编辑  收藏  举报