我想在我的自定义FormView控件中设置一个用于编辑/插入和查看的模板。但我有个奇怪的例外
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);
}
}
}现在,我想将这个用户控件转换为web控件。
如果你能回答我的问题,我将不胜感激。
发布于 2010-08-17 13:13:32
你到底想做什么??
不管你想做什么,你都做错了。
TemplateContainer(typeof(FormView))这是不可能的。
您需要在那里提供您自己的类型,从IDataItemContainer继承。
编辑:
我不建议仅仅因为你想有一个模板来编辑和插入,就把所有这些工作都做好了。最好将相同的内容放在两个模板中。经验告诉我们,随着时间的推移,您将需要不同的功能来编辑和插入。
https://stackoverflow.com/questions/3501623
复制相似问题