Repeater <ItemTemplate> 中的条件锚点?

Conditional anchor in Repeater <ItemTemplate>?

我正在使用转发器打印博客的页面选择器/分页 link。在博客的主视图上,数字 1 周围不应有 link(因为用户已经在主页上,因此是初始结果集)。如果用户单击页面 link,则会添加一个查询字符串 ?page=#,其中 # 是页码。当该页面加载时,该数字现在应该是周围没有 link 的数字。这很有用,人们可以看到他们在哪个页面。

为了给你一个视觉效果,这是批准的设计:

这是我对 Repeater 的标记:

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

在标记或代码隐藏中,我如何首先检查当前的 request/query 字符串,然后有条件地将锚点环绕在数字周围?看起来三元语句/空合并很流行,但这是最佳实践吗?它甚至看起来不可能,但我还是试了一下,但真的无法让它工作...

我也尝试给 <li> 一个 ID 一个 runat="server" 但后面的代码无法识别它。我认为 <ItemTemplate> 中的元素无法访问。

您将锚定到 Repeater.ItemDataBound 事件。您可以找到更多信息 here.

protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{     
     if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
     {
         // Your conditional logic would go here.
     }
}

您的条件示例是在 DataItem 上实例化。所以它看起来像:

((Example)e.Item.DataItem).PropertyInPoco == "Some Value"

希望这对您有所帮助。

所提供的答案均无效,所以我最终保留了所有页码锚点,但通过将颜色设为黑色而不是橙色,向用户当前正在查看的页面添加 css class .