Display message in repeater when row is not found

I am using an ASP.NET repeater and I want to display a No Row Found message when the query returns 0 rows from the database. I know him there in GridView.

Hi

+4
source share
4 answers

If you have a HeaderTemplate or FooterTemplate , you can add any HtmlControl or ServerControl inside any of them, and then programmatically show / hide it in the code.

 <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 is the code

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

Consider using a ListView instead of an EmptyDataTemplate to use when the data source has no data. If you decide to stick with the Repeater control, consider testing your query for records and, if necessary, displaying a Label or Literal that has your “no line” message instead of your repeater.

 if (query.Any()) { repeater.DataSource = query; repeater.DataBind(); } else { noRecordsLiteral.Visible = true; } 
+4
source

Here is a simple example demonstrating a No Record display in a Repeater element.

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

Here is the Behind Code

 protected void Repeater1_PreRender(object sender, EventArgs e) { if (Repeater1.Items.Count < 1) { NoRecords.Visible = true; Repeater1.Visible = false; } } 
+3
source

You can do this by changing the logic in the repeater or by providing data to the relay, which controls the behavior you want. I prefer the repeater to be left without it. If you are familiar with MVC, keep your logic out of sight.

I save space here by using a list as a data source, not a database result, but the principle is the same. You probably have a collection of IDataRecord as a source if you are returning from a database.

 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 assumes the following markup:

 <%@ 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> 
+1
source

All Articles