【问题标题】:form view and external IBindableTemplate表单视图和外部 IBindableTemplate
【发布时间】:2010-08-17 10:54:44
【问题描述】:

我想在我的自定义 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);
        }
    }

已编辑:

在第一步中,我创建了新的用户控件并添加了一个表单视图(“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 控件。

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

【问题讨论】:

    标签: c# asp.net formview ibindabletemplate


    【解决方案1】:

    你到底想做什么?

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

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

    您需要在此处提供自己的类型,继承自 IDataItemContainer

    编辑:

    我不建议仅仅因为您想要 1 个模板进行编辑和插入而投入所有这些精力。最好将相同的内容放在两个模板中。经验表明,随着时间的推移,您将需要不同的编辑和插入功能。

    【讨论】:

    • 亲爱的Jeroen,感谢您的回复,我已经更改了帖子,请您帮我找到解决方案,谢谢。
    • 为什么需要模板?您想使用 1 个模板进行编辑和插入吗?
    • 没错,我对通过属性创建一个独特的模板并不感兴趣,是否可以克隆“插入模板”,并将其设置为编辑的模板?!或某事。像这样 ?谢谢-
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-05
    • 2016-03-21
    相关资源
    最近更新 更多