0

Is there a possible way to skip an item in asp:repeater? I have <ul> and <li>s where I need the ul to be repeated only once. Since it has id and ids must be unique.

Here I need to skip repeating the <ul id="lightgallery"> then continue repeating the <li> tags.

<asp:Repeater ID="rptBlogs" runat="server">
  <ItemTemplate>
      <div class="blog-post">
              <div style="display:none;" id="video<%# Eval("ID") %>">
                  <video class="lg-video-object lg-html5" controls preload="none">
                      <source src="<%# !Eval("ArticleTypeValue").ParseString()%>" type="video/mp4">
                          Your browser does not support HTML5 video.
                  </video>
              </div>
              <div class="post-left-img">
                  <ul id="lightgallery"> //this needs to be skipped 
                      <li class="video" style="position: relative;" data-poster="/<%# Eval("ThumbImage").ParseString() %>" data-sub-html="<%# Eval("Description") %>" data-html="#video<%# Eval("ID") %>" >
                          <a href="javascript:void(0)">
                          <img class="img-responsive" src="/<%# Eval("ThumbImage").ParseString() %>" />
                              <div class="demo-gallery-poster">
                                  <img src="/assets/images/play-button.png">
                              </div>
                          </a>
                      </li>
                  </ul>
              </div>
  </ItemTemplate>
</asp:Repeater>

I know the common sense fact where the structure should be:

<ul>
<asp:repeater>
...
</asp:repeter>
</ul>

But that can't be done because of the HTML structure.

Solved but still open to better ideas. See below for my solution.

Evik Ghazarian
  • 1,803
  • 1
  • 9
  • 24
  • 1
    You don't have to render any markup if you don't want to. You can add conditional logic within your ItemTemplate to inspect the item to determine whether it should be rendered or not. You can check [this answer](https://stackoverflow.com/a/16809133/1139830) to see how to do an if statement in your Web Forms markup. – mason Oct 07 '19 at 17:54
  • @mason I thought about that but how would be the condition if I want it to show but only once. – Evik Ghazarian Oct 07 '19 at 17:55
  • Sounds like your UL shouldn't be within the repeater at all. – mason Oct 07 '19 at 17:57
  • I know the ideal would be
    but because of the html structure that is not possible
    – Evik Ghazarian Oct 07 '19 at 17:58
  • You can add a variable to keep track of whether it's been added already or not. But that looks like you're just avoiding the problem instead of properly spending the time to organize your markup. – mason Oct 07 '19 at 17:59
  • @mason I found a solution. Please leave me your thoughts on this – Evik Ghazarian Oct 07 '19 at 18:10
  • You could dynamically build up the RepeaterItem and bind it to a placeholder, and have your conditional logic for which items to add. – JohnPete22 Oct 07 '19 at 18:18

2 Answers2

0

I used:

<ul id="lightgallery<%# Eval("ID") %>">

Instead of:

<ul id="lightgallery">

Then used jquery starts with selector:

$('[id^=lightgallery]')
Evik Ghazarian
  • 1,803
  • 1
  • 9
  • 24
  • Then you'll have multiple UL's on your page. If that's intended, then great. – mason Oct 07 '19 at 18:11
  • Thank you. It would be ideal not having them but for now I go with this until I find a better way of doing it – Evik Ghazarian Oct 07 '19 at 18:13
  • 1
    I showed you how to do an if statement in markup. You can do that with logic like `if (!markupAlreadyAdded) { /* add markup and set markupAlreadyAdded to true */ }`. Still, I think restructuring your HTML would be much cleaner. – mason Oct 07 '19 at 18:36
0

I think a repeater in your repeater might allow you to get what you are looking for you just may have to tweak the data that you are binding.

Check out this answer Repeater in Repeater

If that doesn't work then you can add an OnDataItemBound (similar to what is shown in the link) to find/remove your control on the server side.

CVarg
  • 69
  • 10