C# 表单视图和外部IBindableTemplate
我想在自定义FormView控件中为编辑/插入和查看设置一个模板。但我有这些奇怪的例外C# 表单视图和外部IBindableTemplate,c#,asp.net,formview,ibindabletemplate,C#,Asp.net,Formview,Ibindabletemplate,我想在自定义FormView控件中为编辑/插入和查看设置一个模板。但我有这些奇怪的例外 无法将“System.Web.UI.LiteralControl”类型的对象强制转换为“System.Web.UI.WebControls.Table”类型。 public class CustomFormView : FormView { [PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof
无法将“System.Web.UI.LiteralControl”类型的对象强制转换为“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”)
现在,我想将此用户控件转换为web控件
如果您能回答我的问题,我将不胜感激。您到底想做什么 不管你想做什么,你都是做错了
TemplateContainer(typeof(FormView))
这是不可能的
您需要在那里提供自己的类型,继承自IDataItemContainer
编辑:
我不建议仅仅因为您希望有一个模板用于编辑和插入,就投入所有这些精力。最好在两个模板中放置相同的内容。经验告诉我们,随着时间的推移,您将需要独特的编辑和插入功能。亲爱的Jeroen,感谢您的回复,我已更改了我的帖子,请您帮助我找到解决方案,谢谢。您为什么需要模板?是否要使用1个模板进行编辑和插入?确切地说,我对按属性创建唯一模板不感兴趣,是否可以克隆“插入模板”,并将其设置为编辑的模板?!还是像这样?谢谢-
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);
}
}
}