Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display message in repeater when no row found

Tags:

c#

asp.net

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

like image 846
Moksha Avatar asked Sep 08 '10 02:09

Moksha


Video Answer


2 Answers

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.Header)
        {
            HtmlGenericControl noRecordsDiv = (e.Item.FindControl("NoRecords") as HtmlGenericControl);
            if (noRecordsDiv != null) {
              noRecordsDiv.Visible = true;
            } 
        }
    }
}
like image 142
jessegavin Avatar answered Sep 23 '22 19:09

jessegavin


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;
}
like image 25
Anthony Pegram Avatar answered Sep 23 '22 19:09

Anthony Pegram