Specify an item placeholder by setting a control's ID property to "itemPlaceholder"

Asad picture Asad · Dec 13, 2009 · Viewed 30.1k times · Source

I have only single "Default.aspx" page and a single ListView Control. Why am I getting this error. Never Happened before

"An item placeholder must be specified on ListView 'ListView1'. Specify an item placeholder by setting a control's ID property to "itemPlaceholder". The item placeholder control must also specify runat="server"."

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TesterConcepts._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 runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>

        </div>
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
            onselecting="ObjectDataSource1_Selecting" SelectMethod="GetItemsCollection" 
            TypeName="TesterConcepts.CutomDataSource">
            <SelectParameters>
                <asp:Parameter Name="items" Type="Object" />
            </SelectParameters>
        </asp:ObjectDataSource>
        <asp:ListView ID="ListView1" runat="server" DataSourceID="ObjectDataSource1" 
            onselectedindexchanged="ListView1_SelectedIndexChanged">
        </asp:ListView>    
    </body>
    </html>

doing this was not helpful even

<asp:ListView ID="ListView1" runat="server" DataSourceID="ObjectDataSource1"
     OnSelectedIndexChanged="ListView1_SelectedIndexChanged"
        ItemPlaceholderID="PlaceHolder1">
</asp:ListView>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

Now it throws this exception

"An item placeholder must be specified on ListView 'ListView1'. Specify an item placeholder by setting a control's ID property to "PlaceHolder1". The item placeholder control must also specify runat="server""

Answer

Ananth picture Ananth · Feb 1, 2011

In ListView, Layout Template is the template which decides the Layout of the data display . It should have an item placeholder tag with runat=”server” attribute.

Since the ListView's LayoutTemplate and ItemTemplate are each defined separately, we need some way to tell the LayoutTemplate, "Hey, for each record you are displaying, put the rendered item markup here." This is accomplished by adding a server-side control with the ID value specified by the ListView's ItemPlaceholderID property.

Ref - http://www.4guysfromrolla.com/articles/122607-1.aspx

Hence U'll have to 1)Define a ItemsTemplate 2)Add a Placeholder in the LayoutTemplate

<tr runat="server" id="itemPlaceholder">
                    </tr>

or

<ItemTemplate>
                <tr>
                    <td>
                        <asp:Label ID="MessageLabel" runat="server" Text='<%# Eval("Item") %>' />
                    </td>
                    <td>
                        <asp:Label ID="URLLabel" runat="server" Text='<%# Eval("URL") %>' />
                    </td>
                </tr>
        </ItemTemplate>

So the final Design will look like

<asp:ListView ID="NoticeItemsListView" runat="server">
            <LayoutTemplate>
                <table width="200px">
                    <tr>
                        <th>
                            Message
                        </th>
                        <th>
                            URL
                        </th>
                    </tr>
                    <tr runat="server" id="itemPlaceholder">
                    </tr>
                </table>
            </LayoutTemplate>
            <ItemTemplate>
                <tr>
                    <td>
                        <asp:Label ID="MessageLabel" runat="server" Text='<%# Eval("Item") %>' />
                    </td>
                    <td>
                        <asp:Label ID="URLLabel" runat="server" Text='<%# Eval("URL") %>' />
                    </td>
                </tr>
            </ItemTemplate>
        </asp:ListView>