Dynamic Load ListView by .ascx

I use this article http://blogs.msdn.com/b/mikeormond/archive/2008/07/26/dynamically-loading-listview-templates.aspx as an example for dynamic loading templates (from .ascx files).

This is the author code:

protected void Page_Load(object sender, EventArgs e) { ListView1.LayoutCreated += new EventHandler(ListView1_LayoutCreated); ListView1.LayoutTemplate = LoadTemplate("LayoutTemplate.ascx"); ListView1.ItemTemplate = LoadTemplate("ItemTemplate.ascx"); } void ListView1_LayoutCreated(object sender, EventArgs e) { //remove the layout template ListView1.Controls.RemoveAt(0); //recreate it Control newLayoutContainer = new Control(); ListView1.LayoutTemplate.InstantiateIn(newLayoutContainer); var userControl = newLayoutContainer.Controls[0]; userControl.ID = "MyLayout"; ListView1.Controls.Add(newLayoutContainer); } 

he gives an error:

ItemTemplate must be defined in ListView 'ListView1'.

after postback.

What happened? Do I have to create a ListView every time I return a message ( http://forums.asp.net/p/1305569/2558209.aspx )?

+4
source share
1 answer

To solve your problem, follow these steps:

 <script runat="server"> protected void ListView_Init(object sender, EventArgs e) { ListView1.ItemTemplate = LoadTemplate("ItemTemplate2.ascx"); } </script> <asp:ListView .... OnInit="ListView_Init"> 
+4
source

Source: https://habr.com/ru/post/1439928/


All Articles