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
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;
}
}
}
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With