使用的HtmlAnchor或ASP.NET超链接的导航在页面命名锚锚标记 [英] Using HtmlAnchor or ASP.NET HyperLink for anchor tag that navigates in-page named anchor

查看:220
本文介绍了使用的HtmlAnchor或ASP.NET超链接的导航在页面命名锚锚标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图呈现一个简单的超链接链接到一个名为锚页面内,例如:

I am trying to render a simple hyperlink that links to a named anchor within the page, for example:

<a href="#namedAnchor">scroll to down</a>

<a name="namedAnchor">down</a>

问题是,当我使用像一个ASP.NET控件 ASP:超链接的HtmlAnchor 的href =#namedAnchor呈现为的href =控制/#namedAnchor(其中控制是包含锚点的用户控件子目录)。这里是code表示的控制下,使用两种类型的锚的控制,这两者有同样的问题:

The problem is that when I use an ASP.NET control like asp:HyperLink or HtmlAnchor, the href="#namedAnchor" is rendered as href="controls/#namedAnchor" (where controls is the subdirectory where the user control containing the anchor is). Here is the code for the control, using two types of anchor controls, which both have the same problem:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Test.ascx.cs" Inherits="TestWebApplication1.controls.Test" %>

<a href="#namedAnchor" runat="server">HtmlAnchor</a>

<asp:HyperLink NavigateUrl="#namedAnchor" runat="server">HyperLink</asp:HyperLink>

生成的源代码如下:

<a href="controls/#namedAnchor">HtmlAnchor</a>

<a href="controls/#namedAnchor">HyperLink</a>

我真的只是想:

<a href="#namedAnchor">HtmlAnchor</a>

<a href="#namedAnchor">HyperLink</a>

我现在用的是的HtmlAnchor 超链接类,因为我想要在$ C更改为其他属性$ C的后面。我不希望引入一个自定义Web控制这一要求,因为我追求的要求不是那么重要,足以证明放弃了传统的ASP.NET控件的链接。好像我应该能够使用ASP.NET控件链接生成所需的链接。

I am using the HtmlAnchor or HyperLink class because I want to make changes to other attributes in the code behind. I do not want to introduce a custom web control for this requirement, as the requirement I'm pursuing is not that important enough to justify abandoning the traditional ASP.NET link controls. It seems like I should be able to use the ASP.NET link controls to generate the desired link.

推荐答案

而不是使用NavigateUrl属性的,只是使用的href属性

Instead of using the NavigateUrl property, just use the href property

<asp:HyperLink href="#namedAnchor" runat="server">HyperLink</asp:HyperLink>

这篇关于使用的HtmlAnchor或ASP.NET超链接的导航在页面命名锚锚标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆