C# asp.net 用户控件,将 htmlAnchor 解析为 href="#"
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1489332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
asp.net user control, getting htmlAnchor resolve to href="#"
提问by Highstead
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 服务器不是一个选项。它在后端被操纵,这只是一个简化。
采纳答案by Ramses
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>
回答by CmdrTallen
Try removing the "runat" attribute and wrapping what you want to link;
尝试删除“runat”属性并包装您想要链接的内容;
<a href="#" >Your Link Text/Image Here</a>
回答by ChaosPandion
This should work.
这应该有效。
<a href="javascript:void(0)">text</a>
<a href="javascript:void(0)">text</a>
This should work.
这应该有效。
<a href="~/#">text</a>
回答by Brendan Kowitz
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", "#");
回答by rick schott
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);
}
回答by Manish
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...
如果我正在做一些意想不到的事情,请纠正我......
回答by Highstead
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 会“智能地”为您解决问题。以上将起作用,但如果有人有更好的解决方案,我会全力以赴。
回答by James
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.
对我有用,但我在类库中使用服务器控件而不是用户控件。我认为它也应该适用于用户控件。
回答by Gx3r0
I ran into the same thing. If you set this on page load, it will work:
我遇到了同样的事情。如果您在页面加载时设置它,它将起作用:
AppRelativeTemplateSourceDirectory = "";
回答by sorpigal
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.
简单的解决方法是在某个全局级别(或至少在 Render 之前)将 Page 对象上的此变量设置为空字符串,尽管如果您的控制结构的某些其他位要求它为空,这可能是一个问题。
I suppose a true fix would be to get Microsoft to make UrlPath.IsRelativeUrl() smarter.
我想真正的解决方法是让 Microsoft 使 UrlPath.IsRelativeUrl() 更智能。