asp.net控件开发基础(21)

   示例代码

       上篇介绍了在asp.net2.0版本下面如何简单的定义数据绑定控件。虽然DataBoundControl为我们提供了便利,我们以后可以从此类开始编写数据绑定控件。但是在2.0版本未到来之前,你已经为自己订制了一些数据绑定控件,既然2.0版本已经提供了数据源控件,你是否有想法,让你原有的控件也升级到同时支持通过设置DataSource属性和数据源控件来获取数据源,这样以后我们就可以省省工作了。这次我们就来讨论这个话题, 让旧版本的数据绑定控件支持数据源控件

一.准备升级数据绑定控件

即使asp.net1.1版本的一些控件也都已经支持数据源控件了,如Repeater,BaseDataList等.但本身这些对象并不是从BaseDataBoundControl和DataBoundControl等类继承下来的,如Repeater其是从Control下继承的一个模板控件,其并不需要这么多从WebControl继承下来的属性,如果你想让它支持数据源控件,你首先会想到改变控件基类,从DataBoundControl开始,这是一个好想法,但可能有些情况下并不允许这么做。上次说到了BaseDataList和DataBoundControl,BaseDataList也支持数据源控件了,所以我认为从此类继承是完全没有问题的。另外的做法就是在不改变原有控件基类的情况下,你还是需要老老实实给原控件添加一些代码支持数据源控件。那么就开始吧.

二.具体实现

本次例子跟上篇相同,相同地方就略过了

1.定义基本成员

整个控件的实现方式跟DataBoundControl实现方式很相似,我们可以看看MSDN中,BaseDataList等基类添加了哪些元素,然后模仿着实现.如果对BaseDataBoundControl和DataBoundControl这两个类成员了解的话,你将对下面成员属性很熟悉,添加这些基本成员


(1)

ContractedBlock.gif ExpandedBlockStart.gif
ExpandedBlockStart.gifContractedBlock.gif       /**//// <summary>
        
/// 该值指示控件是否已经初始化
        
/// </summary>

        protected bool Initialized
ExpandedBlockStart.gifContractedBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return initialized;
            }

        }


       
public string DataMember
ExpandedBlockStart.gifContractedBlock.gif       
{
           
get
ExpandedSubBlockStart.gifContractedSubBlock.gif           
{
               
object member = ViewState["DataMember"];
               
if (member == null)
                   
return string.Empty;
               
else
                   
return (string)member;
           }

           
set
ExpandedSubBlockStart.gifContractedSubBlock.gif           
{
               ViewState[
"DataMember"= value;
               
this.OnDataPropertyChanged();
           }

       }


ExpandedBlockStart.gifContractedBlock.gif        
/**//// <summary>
        
/// 为数据绑定控件提供数据源
        
/// </summary>

       public IEnumerable DataSource
ExpandedBlockStart.gifContractedBlock.gif       
{
           
get
ExpandedSubBlockStart.gifContractedSubBlock.gif           
{
               
return dataSource;
           }

           
set
ExpandedSubBlockStart.gifContractedSubBlock.gif           
{
               
if ((value is IEnumerable) || (value is IListSource) || (value == null))
                   dataSource 
= value;
               
else
                   
throw new Exception("错误的数据源类型");
               OnDataPropertyChanged();
           }

       }


ExpandedBlockStart.gifContractedBlock.gif        
/**//// <summary>
        
/// 数据源控件的 ID 属性
        
/// </summary>

        [DefaultValue(""), IDReferenceProperty(typeof(DataSourceControl))]
        
public virtual string DataSourceID
ExpandedBlockStart.gifContractedBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
object dataSourceID = ViewState["DataSourceID"];
                
if (dataSourceID != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
return (string)dataSourceID;
                }

                
return string.Empty;
            }

            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
this.ViewState["DataSourceID"= value;
                
this.OnDataPropertyChanged();
            }

        }


ExpandedBlockStart.gifContractedBlock.gif        
/**//// <summary>
        
/// 获取是否设置 DataSourceID 属性的值
        
/// </summary>

        protected bool IsBoundUsingDataSourceID
ExpandedBlockStart.gifContractedBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return (DataSourceID.Length > 0);
            }

        }


ExpandedBlockStart.gifContractedBlock.gif        
/**//// <summary>
        
/// 是否需要绑定到其指定的数据源
        
/// </summary>

        protected bool RequiresDataBinding
ExpandedBlockStart.gifContractedBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return requiresDataBinding;
            }

            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                requiresDataBinding 
= value;
            }

        }


ExpandedBlockStart.gifContractedBlock.gif        
/**//// <summary>
        
/// 用于检索数据的 DataSourceSelectArguments 对象。默认为 Empty 值
        
/// </summary>

        protected DataSourceSelectArguments SelectArguments
ExpandedBlockStart.gifContractedBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (selectArguments == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    selectArguments 
= CreateDataSourceSelectArguments();
                }

                
return selectArguments;
            }

        }


(2)上面几个属性涉及到几个方法

ContractedBlock.gif ExpandedBlockStart.gif
ExpandedBlockStart.gifContractedBlock.gif       /**//// <summary>
        
/// 创建空的 DataSourceSelectArguments 对象
        
/// </summary>
        
/// <returns></returns>

        protected virtual DataSourceSelectArguments CreateDataSourceSelectArguments()
ExpandedBlockStart.gifContractedBlock.gif        
{
            
return DataSourceSelectArguments.Empty;
        }


ExpandedBlockStart.gifContractedBlock.gif       
/**//// <summary>
       
/// 如果设置了 DataSourceID 属性且数据绑定控件标记为需要绑定,则调用 DataBind 方法
        
/// OnPreRender中调用
       
/// </summary>

       protected void EnsureDataBound()
ExpandedBlockStart.gifContractedBlock.gif       
{
           
if (RequiresDataBinding && (DataSourceID.Length > 0))
ExpandedSubBlockStart.gifContractedSubBlock.gif           
{
               DataBind();
           }

       }


       

ExpandedBlockStart.gifContractedBlock.gif       
/**//// <summary>
       
/// 在某一基数据源标识属性更改后,将数据绑定控件重新绑定到其数据
       
/// </summary>

       protected virtual void OnDataPropertyChanged()
ExpandedBlockStart.gifContractedBlock.gif       
{
           
if (initialized)
ExpandedSubBlockStart.gifContractedSubBlock.gif           
{
               RequiresDataBinding 
= true;
           }

           currentViewValid 
= false;
       }

上面的几个属性和方法可以一起来看看了,在更改数据源标识时都会调用 OnDataPropertyChanged方法,然后到了EnsureDataBound方法(此方法在OnPreRender方法中调用)在使用数据源控件情况下自动调用DataBind方法。另外Initialized属性会在控件初始化时设置.


2.获取与数据绑定控件关联的 接口
数据源控件实现了IDataSource接口,此接口定义了数据源最基本的元素,数据绑定控件要根据DataSourceID属性从容器中获取与其关联的 接口。如下实现

      //  从容器中获取DataControl
        private  Control FindControl(Control control,  string  controlID)
ExpandedBlockStart.gifContractedBlock.gif        
{
            Control namingContainer 
= control;
            Control dataControl 
= null;
            
if (control != control.Page)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
while ((dataControl == null&& (namingContainer != control.Page))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    namingContainer 
= namingContainer.NamingContainer;
                    
if (namingContainer == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        
throw new HttpException("DataBoundControlHelper_NoNamingContainer");
                    }

                    dataControl 
= namingContainer.FindControl(controlID);
                }

                
return dataControl;
            }

            
return control.FindControl(controlID);
        }


ExpandedBlockStart.gifContractedBlock.gif        
/**/ /// <summary>
        
/// 检索与数据绑定控件关联的 IDataSource 接口
        
/// </summary>
        
/// <returns></returns>

        protected   virtual  IDataSource GetDataSource()
ExpandedBlockStart.gifContractedBlock.gif       
{
           
if (this.currentDataSource != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif           
{
               
return currentDataSource;
           }


           
//获取数据源控件
           IDataSource source = null;
           
string controlID = DataSourceID;
           
if (controlID.Length != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif           
{
               Control control 
= FindControl(this, controlID);
               source 
= control as IDataSource;
           }

           
return source;
       }

3.获取数据源视图

第二步的实现是为此服务的

         private  DataSourceView ConnectToDataSourceView()
ExpandedBlockStart.gifContractedBlock.gif        
{

            
if (!currentViewValid || base.DesignMode)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
                
if ((currentView != null&& currentViewIsFromDataSourceID)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    currentView.DataSourceViewChanged 
-= new EventHandler(this.OnDataSourceViewChanged);
                }


                
this.currentDataSource = GetDataSource();

                
//从DataSource获取数据源
                if (this.currentDataSource == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
this.currentDataSource = new ReadOnlyDataSource(DataSource, DataMember);
                }


                DataSourceView view 
= this.currentDataSource.GetView(DataMember);
                currentViewIsFromDataSourceID 
= IsBoundUsingDataSourceID;
                currentView 
= view;
                
                
if ((currentView != null&& currentViewIsFromDataSourceID)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    currentView.DataSourceViewChanged 
+= new EventHandler(this.OnDataSourceViewChanged);
                }

                currentViewValid 
= true;
            }

            
return currentView;
        }


ExpandedBlockStart.gifContractedBlock.gif        
/**/ /// <summary>
        
/// 获取数据源视图
        
/// </summary>
        
/// <returns></returns>

         protected   virtual  DataSourceView GetData()
ExpandedBlockStart.gifContractedBlock.gif        
{
          
return ConnectToDataSourceView();
        }


请注意ConnectToDataSourceView方法,前后分别在移除和添加一个事件,将 RequiresDataBinding属性设置为true重新绑定,然后再看中间这段代码

                 if  ( this .currentDataSource  ==   null )
ExpandedBlockStart.gifContractedBlock.gif                
{
                    
this.currentDataSource = new ReadOnlyDataSource(DataSource, DataMember);
                }

即当未使用数据源控件时,则就从ReadOnlyDataSource对象通过设置DataSource和DataMember属性来获取 IDataSource 接口,然后才能获取到数据源视图.下面为ReadOnlyDataSource和ReadOnlyDataSourceView的简单实现,在此不做解释.下次再来讲这个东西

ContractedBlock.gif ExpandedBlockStart.gif
   public class ReadOnlyDataSource : IDataSource
ExpandedBlockStart.gifContractedBlock.gif    
{
        
        
private string _dataMember;
        
private object _dataSource;
        
private static string[] ViewNames = new string[0];

       
        
event EventHandler IDataSource.DataSourceChanged
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            add
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
            }

            remove
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
            }

        }


        
        
public ReadOnlyDataSource(object dataSource, string dataMember)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
this._dataSource = dataSource;
            
this._dataMember = dataMember;
        }


        DataSourceView IDataSource.GetView(
string viewName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            IDataSource source 
= _dataSource as IDataSource;
            
if (source != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return source.GetView(viewName);
            }

            
return new ReadOnlyDataSourceView(thisthis._dataMember,DataSourceHelper.ResolveDataSource(this._dataSource, this._dataMember));
        }


        ICollection IDataSource.GetViewNames()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return ViewNames;
        }


    }


 
public class ReadOnlyDataSourceView : DataSourceView
ExpandedBlockStart.gifContractedBlock.gif    
{

        
private IEnumerable dataSource;

        
public ReadOnlyDataSourceView(ReadOnlyDataSource owner, string name, IEnumerable dataSource)
            : 
base(owner, name)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
this.dataSource=dataSource ;
        }


        
protected override IEnumerable ExecuteSelect(DataSourceSelectArguments arguments)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            arguments.RaiseUnsupportedCapabilitiesError(
this);
            
return dataSource;
        }


    }


4.获取数据

接着你便可以在DataBind方法中通过获取到的数据源视图 异步获取数据了,本来我们可以调用其ExecuteSelect方法的,可惜我们无法调用此方法,只好异步调用。接着的PerformDataBinding方法跟上篇实现一样。不再列出

记得在DataBind方法将RequiresDataBinding 属性设置为true

ContractedBlock.gif ExpandedBlockStart.gif
ExpandedBlockStart.gifContractedBlock.gif        /**//// <summary>
        
/// 将数据源绑定到控件
        
/// </summary>

        public override void DataBind()
ExpandedBlockStart.gifContractedBlock.gif        
{
            
if (!IsBoundUsingDataSourceID)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                OnDataBinding(EventArgs.Empty);
            }


            GetData().Select(CreateDataSourceSelectArguments(),
                OnDataSourceViewSelectCallback);
            RequiresDataBinding 
= false;
            MarkAsDataBound();
        }

        
private void OnDataSourceViewSelectCallback(IEnumerable retrievedData)
ExpandedBlockStart.gifContractedBlock.gif        
{
            
if (IsBoundUsingDataSourceID)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                OnDataBinding(EventArgs.Empty);
            }

            PerformDataBinding(retrievedData);
        }

5.重写控件生命周期事件

其中在OnPreRender方法中调用了EnsureDataBound方法,其他方法的话可以发现在很多不同情况下将RequiresDataBinding和Initialized属性设置为True.做了数据绑定的初始化工作。这里估计我也解释不清楚,大家还是了解下控件的生命周期,了解其事件的使用,再理解吧.这里可以参考jessezhao的 这篇翻译

ContractedBlock.gifExpandedBlockStart.gif
        protected override void OnInit(EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
{
            
base.OnInit(e);
            
if (this.Page != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
this.Page.PreLoad += new EventHandler(this.OnPagePreLoad);
                
if (!base.IsViewStateEnabled && this.Page.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
this.RequiresDataBinding = true;
                }

            }

        }


        
private void OnPagePreLoad(object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
{
            initialized 
= true;
            
if (Page != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                Page.PreLoad 
-= new EventHandler(OnPagePreLoad);
                
if (!Page.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    RequiresDataBinding 
= true;
                }

                
if ((Page.IsPostBack && base.IsViewStateEnabled) && (ViewState["DataBound"== null))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    RequiresDataBinding 
= true;
                }

            }

        }


        
protected override void OnPreRender(EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
{
            EnsureDataBound();
            
base.OnPreRender(e);
        }


        
protected override void OnLoad(EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
{
            
this.initialized = true;
            
this.ConnectToDataSourceView();
            
if (this.Page != null && this.ViewState["DataBound"== null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (!this.Page.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
this.RequiresDataBinding = true;
                }

                
else if (base.IsViewStateEnabled)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
this.RequiresDataBinding = true;
                }

            }

            
base.OnLoad(e);
        }

好了,基本代码的编写就完成了,接着你就可以通过设置DataSource属性手动绑定的形式和设置DataSourceID属性获取数据源的形式获取数据了。

这篇可以供参考,如果真要这么做的话,几乎每个原有的数据绑定控件都需要重复编写上面这么多代码。相比之下如 DataBoundControl类和BaseDataList类都已经帮你完成了上面的工作,在有选择的情况下,我们当然不愿意写上面这么多的代码。所以说上面的这堆代码也只供你参考,能够使用新的基类的话,尽量使用,如果真的需要这么做的话,你就需要这么去改你的数据绑定控件。

这篇可能讲的不是很详细,大家如果真的有必要这么做的话,可以仔细看看。不足之处还请大家纠正^_^.
晚了,睡觉去了。

转载于:https://www.cnblogs.com/hunterzou/archive/2008/12/19/1358575.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值