ConditionRacer ConditionRacer - 18 days ago 5x
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" >
... lots of code
... lots more code

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


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


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