2010-08-17 39 views
0

我想为我的自定义FormView控件中的编辑/插入和视图设置一个模板。但我得到这些奇数例外窗体视图和外部IBindableTemplate

Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.Table'.

public class CustomFormView : FormView 
    { 
     [PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof(FormView), BindingDirection.TwoWay)] 
     public IBindableTemplate FormTemplate { get; set; } 

     protected override void OnInit(EventArgs e) 
     { 
      ChangeMode(FormViewMode.Edit); 
      if (FormTemplate != null) 
      { 
       if (CurrentMode == FormViewMode.Edit) 
       { 
        FormTemplate.InstantiateIn(this); 
       } 
      } 
      base.OnInit(e); 
     } 
    } 

编辑:

在第一步骤中

,我创建的新用户控制和引用加到一个FormView( “FV”)

public partial class Form : UserControl 
{ 
    private IBindableTemplate _template = null; 

    [PersistenceMode(PersistenceMode.InnerProperty), 
    TemplateContainer(typeof(FormView), System.ComponentModel.BindingDirection.TwoWay)] 
    public IBindableTemplate FormTemplate { set;get } 

    protected void Page_Init() 
    { 
     if (FormTemplate != null) 
     { 
      FV.InsertItemTemplate = FV.EditItemTemplate = FormTemplate; 
      if (!IsPostBack) FormTemplate.InstantiateIn(FV); 
     } 
    } 
} 

现在,我想将此用户控件转换为我们b控制。

如果您能回复我的问题,我将不胜感激。

回答

1

你究竟想要做什么?

无论你想要做什么,你做错了。

TemplateContainer(typeof(FormView))这是不可能的。

你需要在那里提供你自己的类型,继承自IDataItemContainer

编辑:

只是因为你想为编辑1个模板,并插入我不建议把所有这方面的努力。最好在两个模板中放置相同的内容。经验知道,随着时间的推移,您将需要独特的编辑和插入功能。

+0

亲爱的Jeroen,感谢您的回复,我已更改我的帖子,请您帮我找到解决方案,谢谢。 – Mironline 2010-08-17 21:18:55

+0

为什么你想要一个模板?你想使用1个模板进行编辑和插入吗? – Jeroen 2010-08-17 21:43:36

+0

确切地说,我不是真的有兴趣创建一个独特的模板的属性,是否有可能克隆“插入模板”,并设置编辑的模板?!或某事。喜欢这个 ?谢谢 - – Mironline 2010-08-18 07:43:21