ITemplate container will not render.

C

cmartinbot

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
 
T

Teemu Keiski

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

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top