How do you get a server control HTMLAnchor to have href="#". It keeps resolving the "#" to the control path.
如何让服务器控件HTMLAnchor拥有href =“#”。它不断解析控制路径的“#”。
<a href="#" runat="server" />
resolves to: <a href="../ControlPath/#">
I can't seem to get a google search to give me the results i want so i figured i'd ask here.
我似乎无法得到谷歌搜索给我我想要的结果所以我想我会问这里。
EDIT: Syntax.
编辑:语法。
Removing the runat server is not an option. It's manipulated in the backend, this was just a simplification.
删除runat服务器不是一个选项。它在后端被操纵,这只是一个简化。
10 个解决方案
#1
26
I had the same problem, here's how I could resolve it:
我有同样的问题,这是我如何解决它:
Original code
原始代码
User control:
用户控制:
<a id="foo" runat="server">...</a>
Code behind:
代码背后:
foo.Attributes.Add("href", "#");
Output:
输出:
<a id="..." href="../Shared/Controls/#">...</a>
Updated code
更新的代码
User control:
用户控制:
<asp:HyperLink id="foo" runat="server">...</asp:HyperLink>
Code behind:
代码背后:
foo.Attributes.Add("href", "#");
Output:
输出:
<a id="..." href="#">...</a>
#2
9
I had a similar issue when rendering the page with PageParser.GetCompiledPageInstance() or when the url was rewritten. For some reason the HtmlAnchor always resolved incorrectly (similar to what you have above).
在使用PageParser.GetCompiledPageInstance()呈现页面或重写url时,我遇到了类似的问题。由于某种原因,HtmlAnchor总是错误地解决(类似于你上面的)。
Ended up just using a HtmlGenericControl, since you are manipulating it server-side anyway this may be a possibility for you.
结束只使用HtmlGenericControl,因为无论如何你都在服务器端操作,这可能是你的可能。
HtmlGenericControl anchor = new HtmlGenericControl("a");
anchor.Attributes.Add("href", "#");
#3
2
Brendan Kowitz solution will work, however i was not able to implement it due to the way this control is to operate. I ended up having to hack it as per the following code in the code behind:
Brendan Kowitz解决方案可行,但由于该控制的运行方式,我无法实现它。我最终不得不按照后面代码中的以下代码来破解它:
lnk.Attributes.Add("href",Page.Request.Url.ToString() + "#");
Where lnk is an HtmlAnchor.
lnk是HtmlAnchor。
The reason for this issue in the first place is the control is not in the same directory as the page, and .Net goes about "intelligently" fixing your problem for you. The above will work, though if anyone has a better solution i'm all ears.
首先出现这个问题的原因是控件与页面不在同一目录中,而.Net则“智能地”为您解决问题。以上将是有效的,但如果有人有更好的解决方案,我会全力以赴。
#4
2
Originally I had this as a comment but by request I'm adding it as an answer since nobody else has explained why the original behavior is occurring or how to directly prevent it.
最初我把它作为评论,但是根据要求我将其添加为答案,因为没有其他人解释为什么原始行为正在发生或如何直接阻止它。
The URL rewriting is caused by the method ResolveURL on the Control class. I looked at it in Reflector and found that it will attempt to rewrite anything that it thinks is a relative URL if AppRelativeTemplateSourceDirectory is non-empty.
URL重写是由Control类上的ResolveURL方法引起的。我在Reflector中查看它,发现如果AppRelativeTemplateSourceDirectory非空,它将尝试重写它认为是相对URL的任何内容。
The simple workaround is to set this variable on the Page object to an empty string at some global level (or at least before Render), although this could be an issue if some other bit of your control structure requires it to be empty.
简单的解决方法是在Page对象上将此变量设置为某个全局级别的空字符串(或者至少在Render之前),尽管如果控制结构的某些其他部分要求为空,这可能是一个问题。
I suppose a true fix would be to get Microsoft to make UrlPath.IsRelativeUrl() smarter.
我想真正的解决办法是让微软让UrlPath.IsRelativeUrl()变得更聪明。
#5
1
I ran into the same thing. If you set this on page load, it will work:
我碰到了同样的事情。如果你在页面加载时设置它,它将工作:
AppRelativeTemplateSourceDirectory = "";
#6
0
Try removing the "runat" attribute and wrapping what you want to link;
尝试删除“runat”属性并包装要链接的内容;
<a href="#" >Your Link Text/Image Here</a>
#7
0
This should work.
这应该工作。
<a href="javascript:void(0)">text</a>
This should work.
这应该工作。
<a href="~/#">text</a>
#8
0
Mine too works fine...I have a user control AnchorTag.ascx:
我的工作也很好...我有一个用户控件AnchorTag.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AnchorTag.ascx.cs" Inherits="JavascriptScroll.AnchorTag" %>
<a id="A1" href="#" runat="server" >Anchor Tag</a>
And I included it as :
我把它包括在:
<%@ Register src="AnchorTag.ascx" tagname="AnchorTag" tagprefix="uc1" %>
. . .
。 。 。
<uc1:AnchorTag ID="AnchorTag1" runat="server" />
. .
。 。
And it renders as expected:
它按预期呈现:
<a href="#" id="AnchorTag1_A1">Anchor Tag</a>
Please correct me if I'm doing something which is not expected...
如果我正在做一些不期望的事情,请纠正我...
#9
0
EDIT: Includes nested paths
编辑:包括嵌套路径
My test project renders the correct link for me:
我的测试项目为我呈现了正确的链接:
http://localhost:2279/WebSite1/Default.aspx#
ASPX:
ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register src="control/WebUserControl2.ascx" tagname="WebUserControl2" tagprefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<uc1:WebUserControl2 ID="WebUserControl21" runat="server" />
</form>
</body>
</html>
Control:
控制:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl2.ascx.cs" Inherits="WebUserControl2" %>
<a id="A1" href="<%# URLHelper("~/#") %>" runat="server" >here</a>
Control Code-Behind:
控制代码 - 背后:
protected string URLHelper(string s)
{
return Control.ResolveUrl(s);
}
#10
0
What about this?
那这个呢?
HtmlAnchor errorLink = new HtmlAnchor();
errorLink.InnerText = this.Message;
errorLink.HRef = errorLink.ResolveClientUrl("#" + this.FormControlId);
errorLink.Attributes["rel"] = "form_help";
Works for me but I'm using a Server Control in a Class Library as opposed to a User Control. I think it should work for a User Control as well.
适合我,但我在类库中使用服务器控件而不是用户控件。我认为它也适用于用户控件。
#1
26
I had the same problem, here's how I could resolve it:
我有同样的问题,这是我如何解决它:
Original code
原始代码
User control:
用户控制:
<a id="foo" runat="server">...</a>
Code behind:
代码背后:
foo.Attributes.Add("href", "#");
Output:
输出:
<a id="..." href="../Shared/Controls/#">...</a>
Updated code
更新的代码
User control:
用户控制:
<asp:HyperLink id="foo" runat="server">...</asp:HyperLink>
Code behind:
代码背后:
foo.Attributes.Add("href", "#");
Output:
输出:
<a id="..." href="#">...</a>
#2
9
I had a similar issue when rendering the page with PageParser.GetCompiledPageInstance() or when the url was rewritten. For some reason the HtmlAnchor always resolved incorrectly (similar to what you have above).
在使用PageParser.GetCompiledPageInstance()呈现页面或重写url时,我遇到了类似的问题。由于某种原因,HtmlAnchor总是错误地解决(类似于你上面的)。
Ended up just using a HtmlGenericControl, since you are manipulating it server-side anyway this may be a possibility for you.
结束只使用HtmlGenericControl,因为无论如何你都在服务器端操作,这可能是你的可能。
HtmlGenericControl anchor = new HtmlGenericControl("a");
anchor.Attributes.Add("href", "#");
#3
2
Brendan Kowitz solution will work, however i was not able to implement it due to the way this control is to operate. I ended up having to hack it as per the following code in the code behind:
Brendan Kowitz解决方案可行,但由于该控制的运行方式,我无法实现它。我最终不得不按照后面代码中的以下代码来破解它:
lnk.Attributes.Add("href",Page.Request.Url.ToString() + "#");
Where lnk is an HtmlAnchor.
lnk是HtmlAnchor。
The reason for this issue in the first place is the control is not in the same directory as the page, and .Net goes about "intelligently" fixing your problem for you. The above will work, though if anyone has a better solution i'm all ears.
首先出现这个问题的原因是控件与页面不在同一目录中,而.Net则“智能地”为您解决问题。以上将是有效的,但如果有人有更好的解决方案,我会全力以赴。
#4
2
Originally I had this as a comment but by request I'm adding it as an answer since nobody else has explained why the original behavior is occurring or how to directly prevent it.
最初我把它作为评论,但是根据要求我将其添加为答案,因为没有其他人解释为什么原始行为正在发生或如何直接阻止它。
The URL rewriting is caused by the method ResolveURL on the Control class. I looked at it in Reflector and found that it will attempt to rewrite anything that it thinks is a relative URL if AppRelativeTemplateSourceDirectory is non-empty.
URL重写是由Control类上的ResolveURL方法引起的。我在Reflector中查看它,发现如果AppRelativeTemplateSourceDirectory非空,它将尝试重写它认为是相对URL的任何内容。
The simple workaround is to set this variable on the Page object to an empty string at some global level (or at least before Render), although this could be an issue if some other bit of your control structure requires it to be empty.
简单的解决方法是在Page对象上将此变量设置为某个全局级别的空字符串(或者至少在Render之前),尽管如果控制结构的某些其他部分要求为空,这可能是一个问题。
I suppose a true fix would be to get Microsoft to make UrlPath.IsRelativeUrl() smarter.
我想真正的解决办法是让微软让UrlPath.IsRelativeUrl()变得更聪明。
#5
1
I ran into the same thing. If you set this on page load, it will work:
我碰到了同样的事情。如果你在页面加载时设置它,它将工作:
AppRelativeTemplateSourceDirectory = "";
#6
0
Try removing the "runat" attribute and wrapping what you want to link;
尝试删除“runat”属性并包装要链接的内容;
<a href="#" >Your Link Text/Image Here</a>
#7
0
This should work.
这应该工作。
<a href="javascript:void(0)">text</a>
This should work.
这应该工作。
<a href="~/#">text</a>
#8
0
Mine too works fine...I have a user control AnchorTag.ascx:
我的工作也很好...我有一个用户控件AnchorTag.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AnchorTag.ascx.cs" Inherits="JavascriptScroll.AnchorTag" %>
<a id="A1" href="#" runat="server" >Anchor Tag</a>
And I included it as :
我把它包括在:
<%@ Register src="AnchorTag.ascx" tagname="AnchorTag" tagprefix="uc1" %>
. . .
。 。 。
<uc1:AnchorTag ID="AnchorTag1" runat="server" />
. .
。 。
And it renders as expected:
它按预期呈现:
<a href="#" id="AnchorTag1_A1">Anchor Tag</a>
Please correct me if I'm doing something which is not expected...
如果我正在做一些不期望的事情,请纠正我...
#9
0
EDIT: Includes nested paths
编辑:包括嵌套路径
My test project renders the correct link for me:
我的测试项目为我呈现了正确的链接:
http://localhost:2279/WebSite1/Default.aspx#
ASPX:
ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register src="control/WebUserControl2.ascx" tagname="WebUserControl2" tagprefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<uc1:WebUserControl2 ID="WebUserControl21" runat="server" />
</form>
</body>
</html>
Control:
控制:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl2.ascx.cs" Inherits="WebUserControl2" %>
<a id="A1" href="<%# URLHelper("~/#") %>" runat="server" >here</a>
Control Code-Behind:
控制代码 - 背后:
protected string URLHelper(string s)
{
return Control.ResolveUrl(s);
}
#10
0
What about this?
那这个呢?
HtmlAnchor errorLink = new HtmlAnchor();
errorLink.InnerText = this.Message;
errorLink.HRef = errorLink.ResolveClientUrl("#" + this.FormControlId);
errorLink.Attributes["rel"] = "form_help";
Works for me but I'm using a Server Control in a Class Library as opposed to a User Control. I think it should work for a User Control as well.
适合我,但我在类库中使用服务器控件而不是用户控件。我认为它也适用于用户控件。