Search code examples
asp.netwebformspaginationrepeater

Conditional anchor in Repeater <ItemTemplate>?


I'm using a repeater to print out page selectors / pagination links for a blog. On the main view of the blog, the number 1 should not have a link around it (since the user is already on the main page and thus the initial set of results). If the user clicks a page link this adds a query string of ?page=# where # is the page number. When that page loads, that number should now be the one that does not have a link around it. This is helpful so people can see what page they're on.

To give you a visual, here's the approved design:

enter image description here

Here is my markup for the Repeater:

<asp:Repeater ID="rptBlogPagination" runat="server">
     <ItemTemplate>
          <li>
              <a href="?page=<%# Container.DataItem %>">
                  <%# Container.DataItem %>
              </a>
          </li>
     </ItemTemplate>
</asp:Repeater>

In either the markup or the code-behind, how do I first check the current request/query string and then conditionally wrap an anchor around the number? It looks like ternary statements / null coalescing is popular, but is that best practice? It doesn't even seem possible, but I gave it a try anyway, and really couldn't get it to work...

enter image description here

I also tried giving the <li> an ID an runat="server" but the code behind did not recognize it. I think elements inside of an <ItemTemplate> are not reachable.


Solution

  • Neither of the answers provided worked so I ended up keeping all page numbers anchors, but adding a css class to ones that the user was currently viewing by making the color black instead of orange.