Click here to Skip to main content
15,914,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

If there are no records found in repeater I would like to display a message like "No records found". How can I do that?

Please help me...
Thank you....
Posted
Updated 22-May-14 21:26pm
v2
Comments
DamithSL 23-May-14 3:51am    
you need label message or alert?

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.

XML
<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

C#
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;
            }
        }
    }
}
 
Share this answer
 
create div with visible false like below
ASP.NET
<div id="emptydata" visible="false"  runat="server">
      There is no data currently
</div>

before you binding repeater check how many records you have. if no records you can skip data binding to repeater. set the visible true of emptydata div

you can also use header or footer template
http://www.dotnetcurry.com/showarticle.aspx?ID=271[^]
http://www.mindfiresolutions.com/How-to-show-Empty-Template-in-ASPNET-Repeater-control-1102.php[^]
http://stackoverflow.com/questions/5271500/default-text-for-empty-repeater-control[^]
 
Share this answer
 
v4
C#
public class EmptySource
        {
            public String Message { get; set; }
        }

check if datasource is empty then create instance of EmptySource:-

EmptySource _source = new EmptySource() { Message= "No record found"   };

and set dataSource of 'rpt' with _source like :-
rpt.datasorce = _source;
rpt.databind();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900