DataList empty item template

Posted on 2010-01-05
Last Modified: 2012-05-08
I want to show a message when there's no data in my datalist. I found the following, but, I get an error on the bool. If you have another way to do this, great, but, I'd also like to try to do it this way as a learning experience.
     <asp:DataList id="DataList3" runat="server" DataSourceID="SqlDataSource1">
            <asp:HyperLink ID="ID" runat="server" target="_parent" NavigateUrl='<%# "Employment_detail.aspx?"%>'
                Text='<%# Bind("WJTTL") %>'/>
            <asp:Label ID="lblEmpty"
                Text="Nothing here." runat="server" Visible="true">
                        %>Visible='<%#bool.Parse((SqlDataSource1.Items.Count==0).ToString())%>'>              </asp:Label>

thanks for looking and your comments
Question by:avoorheis
    LVL 52

    Expert Comment

    by:Carl Tawn
    I don't think there is a way to do it declaratively because the SqlDataSource doesn't return details of its records. You will probably have to handle the Selected event of the SqlDataSource and use the AffectedRows property to determine the number of records and then toggle the labels visibility through code.
    LVL 52

    Accepted Solution

    Correction, there is a way to do it with a bit of fiddling. You still need to handle the Selected event of the SqlDataSource, but you can then expose that as a property of your page and bind to it:

            // code-behind stuff
            protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
                hasRecords = e.AffectedRows > 0;

            public bool HasRecords
                get { return hasRecords; }

            bool hasRecords = true;

       // markup
          <asp:Label ID="EmptyLabel" runat="server" Text="Nothing Here" Visible='<%# !this.HasRecords %>' />

    Author Closing Comment


    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Suggested Solutions

    I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
    I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now