inding
{
get { return requiresDataBinding; }
set { requiresDataBinding = value; }
}
/// <summary>
/// 用于检索数据的 DataSourceSelectArguments 对象。默认为 Empty 值
/// </summary>
protected DataSourceSelectArguments SelectArguments
{
get
{
if (selectArguments == null)
{
selectArguments = CreateDataSourceSelectArguments();
}
return selectArguments;
}
}
(2)上面几个属性涉及到几个方法
/// <summary>
/// 创建空的 DataSourceSelectArguments 对象
/// </summary>
/// <returns></returns>
protected virtual DataSourceSelectArguments CreateDataSourceSelectArguments()
{
return DataSourceSelectArguments.Empty;
}
/// <summary>
/// 如果设置了 DataSourceID 属性且数据绑定控件标记为需要绑定,则调用 DataBind 方法
/// OnPreRender中调用
/// </summary>
protected void EnsureDataBound()
{
if (RequiresDataBinding && (DataSourceID.Length > 0))
{
DataBind();
}
}
/// <summary>
/// 在某一基数据源标识属性更改后,将数据绑定控件重新绑定到其数据
/// </summary>
protected virtual void OnDataPropertyChanged()
{
if (initialized)
{
RequiresDataBinding = true;
}
currentViewValid = false;
}
上面的几个属性和方法可以一起来看看了,在更改数据源标识时都会调用OnDataPropertyChanged方法,然后到了EnsureDataBound方法(此方法在OnPreRender方法中调用)在使用数据源控件情况下自动调用DataBind方法。另外Initialized属性会在控件初始化时设置.
2.获取与数据绑定控件关联的IDataSource接口
数据源控件实现了IDataSource接口,此接口定义了数据源最基本的元素,数据绑定控件要根据DataSourceID属性从容器中获取与其关联的IDataSource接口。如下实现
// 从容器中获取DataControl
private Control FindControl(Control control, string controlID)
{
Control namingContainer = control;
Control dataControl = null;
if (control != control.Page)
{
while ((dataControl == null) && (namingContainer != control.Page))
{
namingContainer = namingContainer.NamingContainer;
if (namingContainer == null)
{
throw new HttpException("DataBoundControlHelper_NoNamingContainer");
}
dataControl = namingContainer.FindControl(controlID);
}
return dataControl;
}
return control.FindControl(controlID);
}
/// <summary>
/// 检索与数据绑定控件关联的 IDataSource 接口
/// </summary>
/// <returns></returns>
protected virtual IDataSource GetDataSource()
{
if (this.currentDataSource != null)
{
return currentDataSource;
}
//获取数据源控件
IDataSource source = null;
string controlID = DataSourceID;
if (controlID.Length != 0)
{
Control control = FindControl(this, controlID);
source = control as IDataSource;
}
return source;
}
3.获取数据源视图
第二步的实现是为此服务的
private DataSourceView ConnectToDataSourceView()
|