title: Dynamic Load ListView Template from .ascx
body: <p>I use this <a href="http://blogs.msdn.com/b/mikeormond/archive/2008/07/26/dynamically-loading-listview-templates.aspx" rel="nofollow">http://blogs.msdn.com/b/mikeormond/archive/2008/07/26/dynamically-loading-listview-templates.aspx</a> article as example for dynamic loading templates(from .ascx files).</p>
  
  <p>this is author code:</p>
  
  <pre><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);
      }
  </code></pre>
  
  <p>it gives error:</p>
  
  <p><b>An ItemTemplate must be defined on ListView 'ListView1'.</b></p>
  
  <p>after postback.</p>
  
  <p>What's wrong? Should I create ListView every time I postback ( <a href="http://forums.asp.net/p/1305569/2558209.aspx" rel="nofollow">http://forums.asp.net/p/1305569/2558209.aspx</a> )?</p>