1 messaggio dal 06 marzo 2013
Salve a tutti,

ho creato un mio Web Control che da la possibilità di avere una sorta di struttura/interfaccia base per salvare/copiare un record a database tramite anche Entity Framework.
Internamente a questo controllo, ho creato dei controlli ITemplate cosicché su ogni pagina posso specificare i suoi campi (per esempio campi testo/dropdown e così via) e salvare i dati.
Ecco il codice base della classe:

[ToolboxData("<{0}:EntityEditingPanelControl runat=server></{0}:EntityEditingPanelControl>")]
public class EntityEditingPanelControl : CompositeControl, IPostBackEventHandler
{
    //[TemplateContainer(typeof(MyTemplateContainer))]
    [TemplateContainer(typeof(MyTemplateContainer), System.ComponentModel.BindingDirection.TwoWay)]
    [TemplateInstance(TemplateInstance.Single)]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    [Browsable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public virtual ITemplate ButtonsTemplate { get; set; }

    //[TemplateContainer(typeof(MyTemplateContainer))]
    [TemplateContainer(typeof(MyTemplateContainer), System.ComponentModel.BindingDirection.TwoWay)]
    [TemplateInstance(TemplateInstance.Single)]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    [Browsable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public virtual ITemplate BodyTemplate { get; set; }

    //[TemplateContainer(typeof(MyTemplateContainer))]
    [TemplateContainer(typeof(MyTemplateContainer), System.ComponentModel.BindingDirection.TwoWay)]
    [TemplateInstance(TemplateInstance.Single)]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    [Browsable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public virtual ITemplate QuickViewTemplate { get; set; }

    //some properties and events

    protected override void OnInit(EventArgs e)
    {
        EnsureChildControls();
        //some stuff
        base.OnInit(e);
    }

    private void LoadEntityData()
    {
        //this is the step from that i load data (internally it is called an event from the user control)
    }

    private void SaveEntityData(object key, bool skipConcurrency = false)
    {
        //this is the step from that i save data (internally it is called an event from the user control)
    }

    protected override void OnLoad(EventArgs e)
    {
        //some stuff
        if (!BasePage.IsPostBack)
        {
            //first loading
            LoadEntityData();
        }
        base.OnLoad(e);
    }

    protected override void CreateChildControls()
    {
        EnableViewState = true;
        Controls.Clear();
        if (this.ButtonsTemplate != null)
        {
            ButtonsTemplate.InstantiateIn(pchAdd);
        }

        if (this.BodyTemplate != null)
        {
            BodyTemplate.InstantiateIn(pchBody);
        }

        if (this.QuickViewTemplate != null)
        {
            QuickViewTemplate.InstantiateIn(pchQuickView);
        }
        //creation of all childs
        base.CreateChildControls();
    }

    protected override void Render(HtmlTextWriter writer)
    {

    }

    protected override void RenderContents(HtmlTextWriter writer)
    {
        Render(writer);
    }

    public void RaisePostBackEvent(string eventArgument)
    {
        //throw new NotImplementedException();
    }
}



Però se comincio ad utilizzare all'interno di un template un repeater o un qualsiasi compnente che richiede un datasource e un template interno per la visualizzazione (quindi anche datagrid/datalist etc...), al postback essi diventano "vuoti", o meglio non esiste più un datasource associato e ad ogni post si dovrebbe rifare il binding dei dati.

Dato che non è molto bello dover ad ogni postback rifare il binding, però questa soluzione non funzionerebbe se internamente ho dei campi modificabili tipo TextBox, vorrei cercare dove posso sbagliare nel mio componente.

Grazie in anticipo

Torna al forum | Feed RSS

ASPItalia.com non è responsabile per il contenuto dei messaggi presenti su questo servizio, non avendo nessun controllo sui messaggi postati nei propri forum, che rappresentano l'espressione del pensiero degli autori.