ConditionRacer ConditionRacer - 2 months ago 15
ASP.NET (C#) Question

How do I export an asp:FormView ItemTemplate to an external file

I have an asp:FormView like this:

<asp:FormView ID="foo" runat="server" >
<ItemTemplate>
... lots of code
</ItemTemplate>
<EditItemTemplate>
... lots more code
</EditItemTemplate>
<asp:FormView>


I would like to move the templates out to separate files, if possible. How can I do this?

Answer

Extract the code you have in the templates and add them to separate .ascx files. Then you can do this:

ASPX

<asp:FormView ID="foo" runat="server" OnInit="foo_Init">

Code behind

protected void foo_Init(object sender, EventArgs e)
{
    foo.ItemTemplate = Page.LoadTemplate("~/Controls/MyLayoutTemplate.ascx");
    foo.EditTemplate = Page.LoadTemplate("~/Controls/MyEditTemplate.ascx");
}