ITemplate inside FormView.InsertItemTemlate disappears after postback

531 views Asked by At

I have a UserControl in which i have a FormView.

The form view only has an InsertItemTemplate (I don't need anything else)

<irt:FormView ID="FormViewInsertEvent" DefaultMode="Insert" runat="server" DataKeyNames="EVENT_ID"
    DataSourceID="SqlDataSourceIocEvents">        
    <InsertItemTemplate>
        //Some form elements (text boxes and labels etc.) which are common

        <%if (CustomContent != null)
          { %>
        <hr />
        <asp:PlaceHolder runat="server" ID="PlaceHolderCustomContent"></asp:PlaceHolder>
        <%} %>   

        // Link buttons with insert command
    </InsertItemTemplate>
</irt:FormView>

The code behind goes like this:

public partial class EventControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (CustomContent != null)
        {
            Control ph = FormViewInsertEvent.FindControl("PlaceHolderCustomContent");                
            CustomContent.InstantiateIn(ph);

        }

    }

    [
    DefaultValue(null), 
    PersistenceMode(PersistenceMode.InnerProperty),
    TemplateInstance(TemplateInstance.Single),        
    Browsable(false)
    ]
    public ITemplate CustomContent { 
        get; 
        set; 
    }
}

In the caller (the page) i have something like this (I'm passing the datasource in to the UC and setting the FormView's datasource from the Code behind. There are no issues in this):

<irt:EventControl ID="EventControl" runat="server" DataSourceID="SqlDataSourceIocEvents">   
    <CustomContent>

        Custom Field: 
        <asp:TextBox ID="TextBoxCustomField" runat="server" Text='<%# Bind("CustomField") %>' />

    </CustomContent>
</irt:EventControl>     

My Problem is; when i click on a link button and PostBack, the Custom Content, i.e. the content i've put inside the Template field, disappears.

If I place the asp:PlaceHolder outside the FormView.InsertItemTemplate, there are no issues. however that is not what i need.

I need the ITemplate inside the InsertItemTemplate to retain even after postbacks. It looks like my Template gets added to the Control list of the PlaceHolder, but somewhere between PreRender and Render, these controls are getting removed.

Any ideas?

Thanks Nandun

1

There are 1 answers

0
Nandun On

I got this fixed sometime ago so i don't remember the solution fully (i'm posting this in a hurry in an attempt to help someone) i believe the class and property attributes you see below fixed the issue.

Please note that Irt.Web.ServerControls.PlaceHolder is simply a class that is derived from the System.Web.UI.WebControls.PlaceHolder control. Please mark this if this fixes the issue.

[PersistChildren(true)]
public partial class EventControl : UserControl
{

    protected override void OnInit(EventArgs e)
    {
        if (CustomContent != null)
        {
            CustomContent.InstantiateIn(PlaceHolderCustomContent);
        }
        base.OnInit(e);
    }

    public string ComponentLabel { get; set; }

    public string ComponentValue { get; set; }


    [DefaultValue(null)]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    [TemplateContainer(typeof(Irt.Web.ServerControls.PlaceHolder), System.ComponentModel.BindingDirection.TwoWay)]
    [TemplateInstance(TemplateInstance.Single)]
    [Browsable(false)]
    [Bindable(true, BindingDirection.TwoWay)]
    public ITemplate CustomContent
    { 
        get; 
        set; 
    }

}