DynamicControl/DynamicField 例外的解决

  在使用 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 >

 


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);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Solr 8.7.0版本的schema.xml模板,你可以根据自己的需求进行相应的修改。 ``` <?xml version="1.0" encoding="UTF-8"?> <schema name="example" version="1.6"> <types> <fieldType name="string" class="solr.StrField" sortMissingLast="true" /> <fieldType name="text_general" class="solr.TextField" positionIncrementGap="100"> <analyzer type="index"> <tokenizer class="solr.StandardTokenizerFactory" /> <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" /> <filter class="solr.LowerCaseFilterFactory" /> <filter class="solr.EnglishMinimalStemFilterFactory" /> </analyzer> <analyzer type="query"> <tokenizer class="solr.StandardTokenizerFactory" /> <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" /> <filter class="solr.SynonymGraphFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true" /> <filter class="solr.LowerCaseFilterFactory" /> <filter class="solr.EnglishMinimalStemFilterFactory" /> </analyzer> </fieldType> <fieldType name="date" class="solr.DatePointField" /> <fieldType name="int" class="solr.IntPointField" /> <fieldType name="long" class="solr.LongPointField" /> <fieldType name="float" class="solr.FloatPointField" /> <fieldType name="double" class="solr.DoublePointField" /> <fieldType name="boolean" class="solr.BoolField" /> </types> <fields> <field name="id" type="string" indexed="true" stored="true" multiValued="false" required="true" /> <field name="title" type="text_general" indexed="true" stored="true" multiValued="false" /> <field name="content" type="text_general" indexed="true" stored="true" multiValued="false" /> <field name="date" type="date" indexed="true" stored="true" multiValued="false" /> <field name="price" type="double" indexed="true" stored="true" multiValued="false" /> <field name="location" type="string" indexed="true" stored="true" multiValued="false" /> <field name="category" type="string" indexed="true" stored="true" multiValued="false" /> <field name="is_new" type="boolean" indexed="true" stored="true" multiValued="false" /> <dynamicField name="*_i" type="int" indexed="true" stored="true" multiValued="false" /> <dynamicField name="*_l" type="long" indexed="true" stored="true" multiValued="false" /> <dynamicField name="*_f" type="float" indexed="true" stored="true" multiValued="false" /> <dynamicField name="*_d" type="double" indexed="true" stored="true" multiValued="false" /> <dynamicField name="*_dt" type="date" indexed="true" stored="true" multiValued="false" /> </fields> <uniqueKey>id</uniqueKey> <defaultSearchField>content</defaultSearchField> <solrQueryParser defaultOperator="OR" /> </schema> ``` 其中,该模板定义了一些常用的字段类型,如string、text_general、date、int、long、float、double和boolean等。同时,该模板还定义了一些常用的字段,如id、title、content、date、price、location、category和is_new等,以及动态字段。你可以根据自己的需求进行相应的添加和修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值