views:

37

answers:

1

dear friends

I would like to set the one template for edit/insert and view in my custom FormView control . But i got these odd exception

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);
        }
    }

edited :

in the first step , I created the new user control and added a 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);
        }
    }
}

Now , I want to convert this user control to web control .

I would appreciate it if you could reply my question.

+1  A: 

What exactly are you trying to do??

Whatever it is you're trying to do, you're doing it wrong.

TemplateContainer(typeof(FormView)) This is not possible.

You need to provide your own type there, inheriting from IDataItemContainer.

Edit:

I wouldn't recommend putting all this effort just because you want to have 1 template for edit and insert. Better put the same contents in both templates. Experience learns that over time you will want distinct functionallity for edit and insert.

Jeroen
dear Jeroen , thank you for your response , I've changed my post , would you please help me to find the solution , thank you.
Mironline
Why do you want a template? Do you want to use 1 template for Edit and Insert?
Jeroen
exactly , I am not really interested in creation a unique template by property , is it possible to clone "insert template" , and set that for edit's template ?!or sth. like this ? thanks-
Mironline
read my edit...
Jeroen