Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net List View EmptyItemTemplate not displaying

I'm binding a ListView to a collection of objects which is working fine. Unfortunately when the collection is empty then I'm not getting the text in the EmptyItemTemplate element displayed as I would expect.

Markup code is

        <asp:ListView ID="lvBuildingContactsGrid" runat="server" 
            onitemcommand="lvBuildingContactsGrid_ItemCommand" >
            <LayoutTemplate>
                   <!-- some more html markup -->
             <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
                   <!-- some more html markup -->
            </LayoutTemplate>
            <ItemTemplate>
                   <!-- some item makup -->           
            </ItemTemplate>
            <EmptyItemTemplate>
                   <p> empty text that isn't displaying </p>                
            </EmptyItemTemplate>
        </asp:ListView>

The code behind to bind is

        ContactRoleCollection contactRoles = new ContactRoleCollection();
        contactRoles.ContactRoleSearchByBuildingID(int params);

        lvListView.DataSource = contactRoles;
        lvListView.DataBind();

When the collection returns a count of zero then the EmptyItemTemplate text doesn't display. I have viewed the page source and it isn't rendered at all (rather than being hidden). I have replaced the DataSource object just with null i.e.

lvListView.DataSource = null

Just to test it and it still doesn't work. No text rendered again.

I have had this problem on other pages I have worked on (and given up and done kludge work arounds) so it's obviously just something that I am missing - doing incorrectly.

Any input appreciated

like image 793
Crab Bucket Avatar asked Jan 04 '11 11:01

Crab Bucket


1 Answers

It looks like you're confusing EmptyItemTemplate, which is rendered when there are no more data items to display in the last group of the current page, with EmptyDataTemplate, which is rendered when the data source does not contain any records.

From your question, it seems you need the latter. You should write:

<EmptyDataTemplate>
    <p>Empty text that will be displayed.</p>
</EmptyDataTemplate>
like image 98
Frédéric Hamidi Avatar answered Sep 28 '22 11:09

Frédéric Hamidi