Hi all - I've never created a template control before in ASP.NET, so I'm trying to stumble my way through it! Basically, my template control looks like this:
<%@ Control Language="C#" CodeFile="MyControl.ascx.cs" Inherits="Controls_MyControl" %>
<h2><%= Heading %></h2>
<div id="hide" runat="server">
</div>
<div id="show" runat="server">
</div>
And here's the code-behind:
public partial class MyControl : Control
{
#region Properties
public string Heading { get; set; }
[TemplateContainer(typeof(INamingContainer)), PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate HiddenTemplate { get; set; }
[TemplateContainer(typeof(INamingContainer)), PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate ShownTemplate { get; set; }
#endregion
override protected void CreateChildControls()
{
if (HiddenTemplate != null)
HiddenTemplate.InstantiateIn(hide);
if (ShownTemplate != null)
ShownTemplate.InstantiateIn(show);
base.CreateChildControls();
}
}
This works fine if I don't use any databound controls - so if I do this, it's perfect:
<foo:MyControl id="myControl" runat="server">
<HiddenTemplate>
<h2>Here is some hidden stuff</h2>
</HiddenTemplate>
<ShownTemplate>
<h2>Here is some shown stuff</h2>
</ShownTemplate>
</foo:MyControl>
The problem is, I need to use a databound control in the "ShownTemplate", as follows:
<foo:MyControl id="myControl" runat="server">
<HiddenTemplate>
<h2>Here is some hidden stuff</h2>
</HiddenTemplate>
<ShownTemplate>
<asp:Repeater id="rpt" runat="server">
<ItemTemplate>
<%# Eval("SomeProperty") %>
</ItemTemplate>
</asp:Repeater>
</ShownTemplate>
</foo:MyControl>
How do I enable this type of functionality?
I guess it's like having a Label control nested inside the ItemTemplate of a Repeater - in this case, you need to subscribe to the ItemDataBound event, then locate the Label control using "FindControl"), and bind to it that way. But I'm not sure how I mimic this functionality.
Thanks in advance!