Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I hide a repeater in ASP.NET C# if the DataSource contains no items?
    text
    copied!<p>I have an ASP.NET page that uses a repeater nested within another repeater to generate a listing of data. It's to the effect of the following:</p> <pre><code>&lt;asp:Repeater&gt; &lt;ItemTemplate&gt; &lt;span&gt;&lt;%#Eval("Data1") %&gt;&lt;/span&gt; &lt;!-- and many more --&gt; &lt;asp:Repeater DataSource='&lt;%#Eval("Data2")%&gt;'&gt; &lt;HeaderTemplate&gt; &lt;ul&gt; &lt;/HeaderTemplate&gt; &lt;ItemTemplate&gt; &lt;li&gt;&lt;%#Container.DataItem%&gt;&lt;/li&gt; &lt;/ItemTemplate&gt; &lt;FooterTemplate&gt; &lt;/ul&gt; &lt;/FooterTemplate&gt; &lt;/asp:Repeater&gt; &lt;/ItemTemplate&gt; &lt;/asp:Repeater&gt; </code></pre> <p>In the (C#) code-behind I'm basically using LINQ to pull a listing of information from an XML document and bind that information to the first repeater.</p> <p>Searching for the answer to this, it seems the method is to determine whether the data for the nested repeater is empty. If it is, then you set the visibility of the repeater to false.</p> <p>Unfortunately, I haven't been able to determine how to do that inline, and not in the code-behind (since it won't necessarily work for what I'm doing).</p> <p>Since my pages aren't validating now, because the ul ends up being empty for any items without Data2, and because I'd like to keep using an unordered list, I seek your help.</p> <p>Any ideas?</p> <p>Thanks!</p> <p>UPDATE:</p> <p>If it helps, since it could very well be possible to do in the code-behind, the LINQ is something to this effect:</p> <pre><code>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(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload