// For Syntax Highlighter
Welcome to MSDN Blogs Sign in | Join | Help

Mike Ormond's Blog

In my world, things would be simpler than this...

News

  • Add to Technorati Favorites

    These postings are provided "AS IS" with no warranties, and confer no rights. The use of any script / code samples is subject to the terms specified here.

More on Dynamically Loading ListView Templates

I got quite a few comments on my post on Dynamically Loading ListView Templates so rather than trying to paste code into a comment in reply, I thought I'd paste an update here. Specific issues raised were problems when databinding without a DataSource control and problems on postback. Here's a new version that addresses both those issues:

<%@ Page Language="C#" CodeFile="Default.aspx.cs" Inherits="Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server"></head>
<body>
  <form id="form1" runat="server">

  <asp:ListView ID="ListView1" 
    runat="server" 
    ItemPlaceholderID="MyLayout$itemPlaceholder"
    OnLayoutCreated="ListView1_LayoutCreated">
  </asp:ListView>

  <asp:Button ID="Button1" runat="server" Text="I Do Postbacks" />

  </form>
</body>
</html>
using System;
using System.Linq;
using System.Web.UI;
using System.Xml.Linq;

public partial class Default : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    XDocument xdoc = XDocument.Load(Server.MapPath("~/XmlFile.xml"));

    var query = from x in xdoc.Descendants("book")
                select new { id = (string)x.Attribute("id") };

    ListView1.DataSource = query;
    ListView1.DataBind();
  }

  protected void ListView1_LayoutCreated(object sender, EventArgs e)
  {
    ListView1.LayoutTemplate = LoadTemplate("WebUserControl.ascx");
    ListView1.ItemTemplate = LoadTemplate("WebUserControl2.ascx");

    Control newLayoutContainer = new Control();
    ListView1.LayoutTemplate.InstantiateIn(newLayoutContainer);
    var userControl = newLayoutContainer.Controls[0];
    userControl.ID = "MyLayout";
    ListView1.Controls.Add(newLayoutContainer);
  }
}

Hope that's useful.

Technorati Tags: ,
Posted: Friday, August 22, 2008 12:31 PM by MikeOrmond

Comments

Community Blogs said:

Chris Cavenagh with source for his WPF Photo Print, Rob Houweling on Authentication in SL, Anna Wrochna

# August 23, 2008 7:12 PM

bartekmarnane said:

Hi Mike,

Great example, thank you!

# August 24, 2008 8:23 PM

Den said:

Does anyone know? Is it possible to paste serverside tags like: <%# XPath("text") %> into property. I've done it in page_loag, but still <%# XPath("text") %> in generated html code.

# August 27, 2008 6:27 AM

Gerald said:

Hi thanks for the great tips. I have successfully loaded various custom templates into listview programmatically at runtime.

However, is there anyway I could make a Datapager work with the runtime created layout template? The paging numbers appear correctly (no.pages = no.items/pagesize) etc, however on postback call to update listview, it will look for itemPlaceholder again and says it is not found. I attempted to redo the Layout_Created codes at OnPagePropertiesChanging event, but to no avail.

Hope you can help enlighten me on how to code for paging if the templates are custom coded.

# September 15, 2008 9:17 AM

sushma.korlepara said:

Hi Mike,

          I am also planning to write 70-562 by this year end. Can you suggest me the best book which is useful for this exam

Many Thanks,

Sushma Korlepara.

# September 16, 2008 6:46 AM
New Comments to this post are disabled
Page view tracker