ListView LayoutTemplate does not show when empty asp.net
Asked Answered
K

4

6

I have an <asp:ListView> but for some reason the LayoutTemplate section does not show when the list is empty, although the <EmptyDataTemplate> section shows. The LayoutTemplate contains the headers for the table, and I want to show an empty table when there are no items in the datasource, not just the content of EmptyDataTemplate.

If there is no choice I will copy the LayoutTemplate into EmptyDataTemplate but it seems stupid to have to do this. Ideas?

Keratinize answered 31/3, 2011 at 13:5 Comment(0)
W
14

From the MSDN:

The empty template is displayed in a ListView control when the data source that is bound to the control does not contain any records and the InsertItemPosition property is set to InsertItemPosition.None. The template is rendered instead of the LayoutTemplate template. If the InsertItemPosition property is set to a value other than InsertItemPosition.None, the EmptyDataTemplate template is not rendered.

the key words here are "...the template is rendered instead of the LayoutTemplate template..."

So I think, you have to copy the LayoutTemplate into the EmptyDataTemplate template.

Wangle answered 31/3, 2011 at 13:12 Comment(1)
Indeed. For tables that render their rows in ListViews, I just put the header tags outside the ListView, and the LayoutTemplate just holds the ItemPlaceholder now.Storytelling
I
0

You can also put your into a User Control (.acsx). Then include it in the layout template and the empty template... and it will feel less stupid since you can still manage it in one spot. I know how you feel about copying the same code...seems like something a 5th grader would do. Using a control is a more grown up approach.

Interviewee answered 29/8, 2016 at 12:6 Comment(0)
T
0

In a very simple way you can get both your headers and a message saying that there were no data.

You make your LayoutTemplate like the following idea:

<LayoutTemplate>
  <table>
    <tr>
      <td>a header</td>
      <td>another header</td>
      <td>third header</td>
    </tr>
    <tr runat="server" id="itemPlaceholder">
      <td colspan="3"
        There is no data!
      </td>
    </tr>
  </table>
</LayoutTemplate>

Notice that the tr that is the placeholder (marked by id="itemPlaceholder") actually contains something. It contains what should be shown when there is no data. Then, in code behind, you set the <EmptyTemplate> to be equal to the <LayoutTemplate> (so that you have only one such template to maintain). I do it like this:

Private Sub lvwThings_Init(sender As Object, e As EventArgs) Handles lvwThings.Init
    lvwThings.EmptyDataTemplate = lvwThings.LayoutTemplate
End Sub

The logic then is as follows:

When there is data, i.e. when the actual <LayoutTemplate> is used, the whole <tr runat="server" id="itemPlaceholder">, with the td and text it contains, will be replaced by the <ItemTemplate>.

But, when there is no data, i.e. when the <EmptyTemplate> is used (instead of the <LayoutTemplate>), nothing inside the <EmptyTemplate>is replaced, so everything is shown as it is.

Tishtisha answered 22/3, 2018 at 14:4 Comment(0)
P
0

I just solved this problem when you have InsertItemTemplate with EmptyDataTemplate. Arrcording to MS docs, that's you can't have both. So I decided to create new tag in InsertItemTemplate. You can preview my example code here.

   <InsertItemTemplate>
        <% if (CheckEmptyTable())
            { %>
              <tr> 
                 <td colspan="6">No data founds。</td>
              </tr>
        <% } %>
         // Your insert template input here 
        <tr style="">
          
        </tr>
    </InsertItemTemplate>

My result image: enter image description here

Preponderate answered 22/7, 2022 at 4:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.