The ListView control: A tip how to insert ItemTemplate into LayoutTemplate

  1. Add an HTML element to the <LayoutTemplate> container (your item template will be nested in this element).
  2. Set the element's ID to itemPlaceholder. Otherwise the ASP.NET template parser will throw an error.

Example

Consider the following fragment of a web form:
<asp:ListView ID="ListView1" DataSourceID="XmlDataSource1"
              runat="server">
  <LayoutTemplate>
    <div runat="server" id="itemPlaceholder" />
  </LayoutTemplate>
  <ItemTemplate>
    <div style="clear:both">
      <div style="float:left"><%# Eval("title")%></div>
      <div style="float:left"><%# Eval("author")%></div>
    </div>
  </ItemTemplate>
</asp:ListView>
This markup will output HTML markup similar to the one below:
<div> <!-- results from <div runat="server" id="itemPlaceholder" /> -->
  <div style="clear:both">
    <div style="float:left">Secrets of Silicon Valley</div>
    <div style="float:left">Sheryl Hunter</div>
  </div>
  <div style="clear:both">
    <div style="float:left">Straight Talk About Computers</div>
    <div style="float:left">Dean Straight</div>
  </div>
</div>

Comments

Popular posts from this blog

CREATE DATABASE (SMO)

Enable JSON serialization in your Visual Studio 2010 project

jQuery | remove inline style