如何通过转发器添加锚标签并跳转到页面上的特定位置?

时间:2022-05-31 21:14:10

I want to use repeater to populate anchors and links to that anchors dynamically.
I know for statistic case we can do this:

我想使用转发器动态填充锚点和链接到锚点。我知道在统计案例中我们可以这样做:

<a href="#anchor">Link Text</a>

and the anchor :

和锚:

<a name="anchor"></a>

Now, to create the link dynamically I use:

现在,要动态创建链接我使用:

<asp:Repeater ID="LinkRepeater" runat="server">
 <ItemTemplate>
    <asp:HyperLink ID="HyperLink1" runat="server"  class="TopMenuBarLink" NavigateUrl='<%# Eval("Link")%>'>
      <%# Eval("Title")%>
    </asp:HyperLink>
  </ItemTemplate>
</asp:Repeater>

But when I run the program, on the website, use inspect element I get this for the links:

但是当我在网站上运行程序时,使用inspect元素我得到了这个链接:

<a id="_ctl0_viewCompanies_LinkRepeater_HyperLink1_0" class="TopMenuBarLink" href="Mypath/MyAnchor">My title text</a>

How do I get it in the right form:

我如何以正确的形式获得它:

 <a id="_ctl0_viewCompanies_LinkRepeater_HyperLink1_0" class="TopMenuBarLink" href="#MyAnchor">My title text</a>

2 个解决方案

#1


1  

Do you have a specific reason that you're using a server control for your hyperlink? why not use a normal anchor element.

您是否有特定原因要为超链接使用服务器控件?为什么不使用普通的锚元素。

<asp:Repeater ID="LinkRepeater" runat="server">
 <ItemTemplate>
    <a class="TopMenuBarLink" href='#<%# Eval("Link")%>'>
      <%# Eval("Title")%>
    </a>
  </ItemTemplate>
</asp:Repeater>

#2


0  

I would try this (add the # symbol before the ASP.Net code bracket):

我会尝试这个(在ASP.Net代码括号前添加#符号):

NavigateUrl='#<%# Eval("Link")%>'

#1


1  

Do you have a specific reason that you're using a server control for your hyperlink? why not use a normal anchor element.

您是否有特定原因要为超链接使用服务器控件?为什么不使用普通的锚元素。

<asp:Repeater ID="LinkRepeater" runat="server">
 <ItemTemplate>
    <a class="TopMenuBarLink" href='#<%# Eval("Link")%>'>
      <%# Eval("Title")%>
    </a>
  </ItemTemplate>
</asp:Repeater>

#2


0  

I would try this (add the # symbol before the ASP.Net code bracket):

我会尝试这个(在ASP.Net代码括号前添加#符号):

NavigateUrl='#<%# Eval("Link")%>'