Welcome

首页 / 软件开发 / .NET编程技术 / DynamicControl/DynamicField例外的解决

DynamicControl/DynamicField例外的解决2012-01-04 博客园 沐枫在使用 DynamicField / DynamicControl 时,经常在PostBack时,会出现一个例外:

The DynamicControl/DynamicField needs to exist inside a data control that is bound to a data source that supports Dynamic Data.

中文为:

绑定到支持动态数据的数据源的数据控件内必须存在 DynamicControl/DynamicField

具体发生原因暂时不太清楚,估计是PostBack的事件流程和页面正常加载不一致。当PostBack后,数据控件(如DetailsView, GridView, ListView 等)在状态加载时, 会把所有的 Fileds或Columns初始化一次,而这一次,由于数据源没有绑定,所以找不到相关的 MetaTable。在这种情况下,DynamicControl 和 DynamicField 都是丢出上述的例外。

不过,从应用上来看,如果数据绑定做的合理的话(比如在InitComplete以前绑定),是不会出现这个问题的。但是一旦出了这个问题,解决起来就比较麻烦了,因为常常有些人习惯于在Load中绑定数据,要修改的话,常常会牵连一大堆的代码。希望对这个问题比较了解的朋友多多指教。

目前我采用的方法,可以不改变原有的绑定流程,可以跟以前的Eval、Bind等一样使用,可以让大家试试。在使用中如有发现有Bug,请发邮件告诉我。因为我最近几个月都在应用 DynamicFieldTemplates 来开发应用程序,对这些问题比较关心。

使用这种方法,可以让一些程序经验不足的人,也能避开这个错误。

我分别从DynamicControl 和 DynamicField 继承了新的类 DdControl 和 DdField,然后在初始化时,判断是否存在MetaTable,如果不存在,则不再初始化。然后,程序中原本所有采用 DynamicControl/DynamicField的地方,都换成DdControl/DdField。

DdControl/DdField的源码如下:

namespace Common{  public class DdField : DynamicField  {    public override void InitializeCell(System.Web.UI.WebControls.DataControlFieldCell cell, System.Web.UI.WebControls.DataControlCellType cellType, System.Web.UI.WebControls.DataControlRowState rowState, int rowIndex)    {      // HACK: Fix bug for: The DynamicControl/DynamicField needs to exist inside a data control that is bound to a data source that supports Dynamic Data.      if (base.Control.FindMetaTable() == null)        return;        base.InitializeCell(cell, cellType, rowState, rowIndex);    }    }}  namespace Common{  public class DdControl : DynamicControl  {    protected override void OnInit(EventArgs e)    {      // HACK: Fix bug for: The DynamicControl/DynamicField needs to exist inside a data control that is bound to a data source that supports Dynamic Data.      if (this.FindMetaTable() != null)        base.OnInit(e);    }  }}
使用时,只要在web.config中配置好前缀,就可以直接使用了。如:

<!-- 假设上述的程序生成 Common.dll --><add tagPrefix="asp" namespace="Common" assembly="Common"/>  <DetailsView > <Fields>  <asp:DdField DataField="Name" />   </Fields></DetailsView>