How to hide a repeater in ASP.NET C # if there are no elements in the DataSource?

I have an ASP.NET page that uses a repeater nested in another repeater to create a list of data. This leads to the following:

<asp:Repeater> <ItemTemplate> <span><%#Eval("Data1") %></span> <!-- and many more --> <asp:Repeater DataSource='<%#Eval("Data2")%>'> <HeaderTemplate> <ul> </HeaderTemplate> <ItemTemplate> <li><%#Container.DataItem%></li> </ItemTemplate> <FooterTemplate> </ul> </FooterTemplate> </asp:Repeater> </ItemTemplate> </asp:Repeater> 

In code (C #), I mainly use LINQ to pull a list of information from an XML document and associate this information with the first relay.

Looking for an answer to this question, it seems that the method should determine if the data is empty for the nested repeater. If so, you set the visibility of the repeater to false.

Unfortunately, I was not able to determine how to do this, and not in the code (since it will not necessarily work for what I am doing).

Since my pages are not being checked now, because ul ends up empty for any elements without Data2, and because I want to continue using the unordered list, I am turning to you for help.

Any ideas?

Thanks!

UPDATE:

If this helps, since it is entirely possible to do in the code, LINQ is something with this:

 var x = from y in z select new { Data1 = d, // etcetera Data2 = (from j in k where j.Value != String.Empty select j.Value).ToList() }; blah.DataSource = x; blah.DataBind(); 
+7
c # repeater
source share
9 answers

This will not hide the repeater completely, but you can subclass the Repeater control to include an empty data template in the form of a GridView:

 using System; using System.Web.UI; using System.Web.UI.WebControls; public class EmptyCapableRepeater : Repeater { public ITemplate EmptyDataTemplate { get; set; } protected override void OnDataBinding ( EventArgs e ) { base.OnDataBinding( e ); if ( this.Items.Count == 0 ) { EmptyDataTemplate.InstantiateIn( this ); } } } 

You can use it in your .aspx as follows:

 <custom:EmptyCapableRepeater runat="server" ID="rptSearchResults"> <ItemTemplate> <%# Eval( "Result" )%> </ItemTemplate> <SeparatorTemplate> <br /> </SeparatorTemplate> <EmptyDataTemplate> <em>No results were found.</em> </EmptyDataTemplate> </custom:EmptyCapableRepeater> 
+10
source share

Try something like:

 <asp:Repeater runat="server" DataSource='<%#Eval("Data2")%>' Visible='<%# ((IEnumerable)Eval("Data2")).GetEnumerator().MoveNext() %>'> 

for nested repeater

+3
source share

Why not use a ListView? It offers most of the features, including the EmptyDataTemplate.

+2
source share

use this:

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

When you execute your LINQ query, check its Count property (providing it with a list of some kind). If its 0, then just turn the Visible property to false.

0
source share

As far as I know, you should do it through codebehind, just use the ItemDataBound event to handle it, you can leave almost everything that is, just enter some logic that receives the data set and determines if it has records if you don’t hide the repeater .

0
source share

I do not think that what you do will work. I get an error when trying to install a DataSource, as you are trying to do; however in the code behind you do this:

Assuming you have added a listener to the parent relay of ItemDataBoundEvent, you need to modify your linq request a bit so as not to use an anonymous type (create a protected class with your properties). In the case of mjy, I use dto as the class name.

 void rep1_ItemDataBound(object sender, RepeaterItemEventArgs e) { Repeater rep2 = (Repeater)e.Item.FindControl("rep2"); rep2.DataSource = ((dto)e.Item.DataItem).y; rep2.DataBind(); } 

I would like to know why you think you cannot solve this in code.

0
source share

I know this is an old thread and the answer above is a very nice solution, but I had a similar problem and I found another changing simple solution which I thought I would share too. It only checks the exact and displays the same.

Just change the footer template to:

 <FooterTemplate> <li style="display:none;">This will not show.</li></ul> </FooterTemplate> 

Or if you use tables:

 <FooterTemplate> <tr> style="display:none;"><td>But something must be in here.</td></tr></table> </FooterTemplate> 

Hope this helps someone!

0
source share

In the OnItemDataBound event OnItemDataBound set the visibility to false if ItemType is Header and set the visibility to true if ItemType is Item .

0
source share

All Articles