ITemplate container will not render.

Discussion in 'ASP .Net Building Controls' started by cmartinbot, Feb 12, 2007.

  1. cmartinbot

    cmartinbot Guest

    Hello and TIA.

    I have a very simple server control that has one property that needs
    to be rendered in an ITemplate. I can't seem to figure out why it
    won't render with the <%# Container.Value%> syntax inside the
    declarative template.

    The container control for the template is being instantiated
    correctly.

    It should be said that this control lives within a Repeater
    ItemTemplate which I suspect is the reason for this not working.

    Relative Code:
    ---

    [ParseChildren(true)]
    public class CustomData : WebControl, INamingContainer
    {
    private ITemplate content;
    private string currentValue;

    protected override void CreateChildControls()
    {
    if( !ChildControlsCreated )
    {
    IDataRetriever retriever = DataRetriever.Create(dataType, article,
    forceFetch, label);

    currentValue = retriever.ValueString;

    InstantiateTemplate(content);

    ChildControlsCreated = true;
    }
    }

    private void InstantiateTemplate(ITemplate template)
    {
    if(template == null)
    {
    template = new DefaultTemplate( currentValue );
    }

    DataContainer container = new DataContainer(currentValue);
    template.InstantiateIn(container);
    Controls.Add( container );
    }

    #region Properties

    [PersistenceMode( PersistenceMode.InnerProperty )]
    [TemplateContainer(typeof(DataContainer))]
    [Browsable(false)]
    public ITemplate Content
    {
    get { return content; }
    set { content = value; }
    }

    #endregion
    }
    }

    public class DataContainer : Control, INamingContainer
    {
    private string value;

    public DataContainer(string value)
    {
    this.value = value;
    }

    public string Value
    {
    get { return value; }
    set { this.value = value; }
    }
    }

    ASPX:
    ---
    <cc1:CustomData ID="CustomData2" DataType="ShortString" runat="server"
    Label="City">
    <Content>(CityIsHereButNotShowing[<%# Container.Value%>])</Content>
    </cc1:CustomData>

    Please help!!!

    Thanks again,
    Chris Martin
     
    cmartinbot, Feb 12, 2007
    #1
    1. Advertisements

  2. cmartinbot

    Teemu Keiski Guest

    Have you overridden DataBind method to recreate child controls?

    Something like:

    public override void DataBind()
    {
    ChildControlsCreated = false;
    CreateChildControls();
    ChildControlsCreated = true;
    base.DataBind();

    }

    You don't also need to check ChildControlsCreated in CreateChildControls,
    calling EnsureChildControls will do that. And in CreateChildControls the
    first line should be Controls.Clear();
     
    Teemu Keiski, Feb 14, 2007
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.