tags:

views:

40

answers:

3

Hi,

I am using ASP.NET repeater and I want to display No Row Found message when query return 0 rows from database. I know its there in GridView.

Regards

A: 

Think about using the ListView control instead that has an EmptyDataTemplate for use when the data source has no data. If you opt to stick with the Repeater control, think about testing your query for records and optionally displaying a Label or Literal that has your "no row found" message instead of your repeater.

if (query.Any())
{
    repeater.DataSource = query;
    repeater.DataBind();
}
else
{
    noRecordsLiteral.Visible = true;
}
Anthony Pegram
+1  A: 

If you have a HeaderTemplate or a FooterTemplate defined, you could add any HtmlControl or ServerControl inside of either of them and then programatically show/hide it in the codebehind.

<asp:Repeater id="Repeater1" runat="server" OnItemDataBound="">
 <HeaderTemplate>
  <h1>My Repeater Data</h1>
  <div id="NoRecords" runat="server" visible="false">
    No records are available.
  </div>
 </HeaderTemplate>
 <ItemTemplate>
 ...
 </ItemTemplate>
</asp:Repeater>

Here's the code behind

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (Repeater1.Items.Count < 1)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
            HtmlGenericControl noRecordsDiv = (e.Item.FindControl("NoRecords") as HtmlGenericControl);
            if (noRecordsDiv != null) {
              noRecordsDiv.Visible = true;
            } 
        }
    }
}
jessegavin
A: 

You could do this by altering the logic in the repeater or by providing data to the repeater that drives the behavior you want. I prefer that the repeater is left out of it. It's a keep your logic out of the view thing if you are familiar with MVC.

I am saving space here by using a List as the data source instead of a database result but the principle is the same. You would probably have a collection of IDataRecord as your source if you are returning from a DB.

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Linq;

namespace StackOverflowRepeater
{
    public partial class Default : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            repeater.ItemDataBound += HandleRepeaterItemDataBound;

            var data = new List<string>();

            if (!data.Any())    // could also be data.Count < 1
            {
                data.Add("No Row Found");
            }

            repeater.DataSource = data;
            repeater.DataBind();

            base.OnInit(e);
        }

        void HandleRepeaterItemDataBound (object sender, RepeaterItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.AlternatingItem) || (e.Item.ItemType == ListItemType.Item))
            {
                var span = (HtmlGenericControl) e.Item.FindControl("output");
                span.InnerText = e.Item.DataItem.ToString();
            }
        }
    }
}

This is assuming the following mark-up:

<%@ Page Language="C#" Inherits="StackOverflowRepeater.Default" %>
<html>
<body>
    <form runat="server">
        <asp:Repeater id='repeater' runat="server">
            <ItemTemplate>
                <span id='output' runat="server" />
            </ItemTemplate>
        </asp:Repeater>
    </form>
</body>
</html>
Justin