External ITemplates and Hierarchical Databinding

Ever wish you could declare a template outside of the control you were defining the template for? We always get requests to have FormView's InsertItem template fall back on the EditItemTemplate and vice versa. That would be easy if we could do what was mentioned above.

Consider:

<asp:FormView ID="myFormView" runat="server" DefaultMode="Edit" 
    EditItemTemplate="editTemplate" 
    InsertItemTemplate="editTemplate">
</asp:FormView>
 
<asp:Template runat="server" ID="editTemplate">
    Name : <asp:TextBox runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
    Age : <asp:TextBox runat="server" Text='<%# Bind("Age") %>'></asp:TextBox>
</asp:Template>

Then you could do things like hierarchical databinding pretty easily; just define the template in terms of itself. Today, properties typed as ITemplate are treated specially by the ASP.NET parser, and what is written above will not work.

How would you do this with what asp.net offers now? Well check out this sample:

<cc:SpecialRepeater runat="server" ItemTemplateID="folderTemplate" DataSource='<%# GetDirectories() %>' />        
<cc:Template runat="server" ID="folderTemplate">
    <ItemTemplate>
        <ul>
            <li>
                <%# Eval("Name") %>
                <cc:SpecialRepeater runat="server" DataSource='<%# GetDirectories((string)Eval("FullName")) %>' ItemTemplateID="folderTemplate" />
                <ul>
                    <cc:SpecialRepeater runat="server" DataSource='<%# GetFiles((string)Eval("FullName")) %>' ItemTemplateID="fileTemplate" />
                </ul>
            </li>
        </ul>
    </ItemTemplate>
</cc:Template>
 
<cc:Template runat="server" ID="fileTemplate">
    <ItemTemplate>
        <li> <%# Eval("Name") %>
        </li>
    </ItemTemplate>
</cc:Template>

We have a SpecialRepeater that understands how to hookup template properties through their ID (it's just FindControl) and a Template control that defines our file template and folder template. We define the folder template in terms of itself. Can you think of any more uses for something like this?

Get the code here.

What do you think?

Published Monday, March 16, 2009 11:25 PM by davidfowl

Comments

# External ITemplates and Hierarchical Databinding

Tuesday, March 17, 2009 4:02 AM by What's New

Ever wish you could declare a template outside of the control you were defining the template for? We

# Dew Drop - March 17, 2009 | Alvin Ashcraft's Morning Dew

Pingback from  Dew Drop - March 17, 2009 | Alvin Ashcraft's Morning Dew

# re: External ITemplates and Hierarchical Databinding

Wednesday, March 18, 2009 8:38 AM by Nemesis116

Looks very clean.

# Interesting Finds: 2009-03-18

Wednesday, March 18, 2009 11:13 AM by Bolik

SOAAgents:当网格遇上SOA在云中做一小时测试为什么我们要放弃Subversion如何使用VMDKtoVHDConverter转换虚拟硬盘?谈表达式树的缓存(3):使用...

# Interesting Finds: 2009-03-18

Wednesday, March 18, 2009 11:14 AM by Bolik

SOA Agents:当网格遇上SOA 在云中做一小时测试 为什么我们要放弃Subversion 如何使用VMDK to VHD Converter转换虚拟硬盘? 谈表达式树的缓存(3):使用前缀树 Unexpected

# re: External ITemplates and Hierarchical Databinding

Wednesday, March 18, 2009 11:50 AM by Gufran Sheikh

Hi,

Liked very much, good and simple logic. this shows that sometimes we can make things easy instead of going for complex one.

# re: External ITemplates and Hierarchical Databinding

Friday, November 13, 2009 6:09 AM by Ricardo Peres

The download location is unavailable. Could you please correct that? Thanks!

# re: External ITemplates and Hierarchical Databinding

Sunday, November 15, 2009 2:49 PM by davidfowl

The download link is fixed now. Thanks for reporting this!

Leave a Comment

(required) 
(required) 
(optional)
(required)