locked
ListView empty data message RRS feed

  • Question

  • User1510859543 posted

    I am trying to conditionally change the message that displays when a ListView datasource returns no data.  My EmptyDataTemplate is shown below.

                <EmptyDataTemplate>
                    <table id="Table1" runat="server">
                        <tr>
                            <td>&nbsp;</td>
                        </tr>
                        <tr>
                            <td class="nodata">
                                <asp:Label ID="LblNoData" runat="server" Text="This client has no meals. Select one of these options to create meals."></asp:Label>
                            </td>
                        </tr>
                    </table>
                </EmptyDataTemplate>
    

    I have tried code similar to below in the Databound, Itemdatabaound and Prerender events with no success.

            If Not lvActualMeals.FindControl("LblNoData") Is Nothing Then
                If txtSearchFor.Text <> "" Then
                    Dim lbl As Label = CType(lvActualMeals.FindControl("LblNoData"), Label)
                    lbl.Text = "The item you searched for was not found in these meals."
                End If
            End If

    Thursday, August 4, 2016 6:45 PM

Answers

  • User2103319870 posted

    You can use the ItemCreated Event to access the controls inside EmptyDataTemplate

        ''' <summary>
        ''' Handles the ItemCreated event of the productList control.
        ''' </summary>
        ''' <param name="sender">The source of the event.</param>
        ''' <param name="e">The <see cref="ListViewItemEventArgs"/> instance containing the event data.</param>
        Protected Sub productList_ItemCreated(sender As Object, e As ListViewItemEventArgs) Handles productList.ItemCreated
            If e.Item.ItemType = ListViewItemType.EmptyItem Then
                Dim lbl As Label = TryCast(e.Item.FindControl("LblNoData"), Label)
                If lbl IsNot Nothing Then
                    lbl.Text = "New value"
                End If
            End If
        End Sub

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 4, 2016 8:16 PM