【问题标题】:ASP.NET: How to convert <A> or HtmlAnchor to static text?ASP.NET:如何将 <A> 或 HtmlAnchor 转换为静态文本?
【发布时间】:2010-09-16 02:55:15
【问题描述】:

我有一个可以输出一系列项目的中继器:

<asp:repeater ... runat="Server">
   <itemtemplate>
      <a href="<%# GetItemLink(...) %>"><%# GetItemText %></a>
   <itemtemplate>
<asp:repeater>

但有些项目没有关联的链接,所以我不希望它们是可点击的。我尝试将其设为runat=server HtmlAnchor,并将htmlAnchor.Disabled = true 设置为这些项目实际上不应该有链接 - 但它们仍然可以被点击(它只是使文本变灰)

我知道在过去我会怎么做:

<% If IsLink Then %>
   <A href="<% =GetItemLink%">
<% End If %>
   <% =GetItemText %>
<% If IsLink Then %>
   </A>
<% End If %>

但那是混乱的混合代码和 html ASP 方式。什么是 ASP.NET 方式?

【问题讨论】:

    标签: asp.net html


    【解决方案1】:

    使用 控件,如果没有提供链接,它会正常显示文本。


    已编辑以包含示例:

    <asp:repeater ... runat="Server">
       <itemtemplate>
          <asp:HyperLink ... runat="server" NavigateUrl="<%# GetItemLink(...) %>"> <%# GetItemText %></asp:HyperLink>
       <itemtemplate>
    <asp:repeater>
    

    在上面的例子中,锚标签无论如何都会被渲染成 html,但是如果 NavigateUrl 属性是一个空字符串,那么根本就不会有href 并且我曾经使用过的每个浏览器都会将文本呈现在一个类似于 span 的方式(因此请注意 上的自定义样式)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-07
      • 2011-10-28
      • 2016-11-10
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 2012-10-22
      • 2011-02-04
      相关资源
      最近更新 更多