asp.net user control, getting htmlAnchor resolve to href=”#”

How do you get a server control HTMLAnchor to have href=”#”. It keeps resolving the “#” to the control path.

<a href="#" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" runat="server" />
resolves to: <a href="../ControlPath/#" rel="nofollow noreferrer noopener">

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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/#" rel="nofollow noreferrer noopener">...</a>

Updated code

User control:

<asp:HyperLink id="foo" runat="server">...</asp:HyperLink>

Code behind:

foo.Attributes.Add("href", "#");

Output:

<a id="..." href="#" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener">...</a>

Method 2

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).

Ended up just using a HtmlGenericControl, since you are manipulating it server-side anyway this may be a possibility for you.

HtmlGenericControl anchor = new HtmlGenericControl("a");
anchor.Attributes.Add("href", "#");

Method 3

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.

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.

I suppose a true fix would be to get Microsoft to make UrlPath.IsRelativeUrl() smarter.

Method 4

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:

lnk.Attributes.Add("href",Page.Request.Url.ToString() + "#");

Where lnk is an 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.

Method 5

I ran into the same thing. If you set this on page load, it will work:

AppRelativeTemplateSourceDirectory = "";

Method 6

Try removing the “runat” attribute and wrapping what you want to link;

<a href="#" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" >Your Link Text/Image Here</a>

Method 7

This should work.

<a href="javascript:void(0)" rel="nofollow noreferrer noopener">text</a>

This should work.

<a href="~/#" rel="nofollow noreferrer noopener">text</a>

Method 8

Mine too works fine…I have a user control AnchorTag.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AnchorTag.ascx.cs" Inherits="JavascriptScroll.AnchorTag" %>
<a id="A1" href="#" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" 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="#" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" id="AnchorTag1_A1">Anchor Tag</a>

Please correct me if I’m doing something which is not expected…

Method 9

EDIT: Includes nested paths

My test project renders the correct link for me:

 http://localhost:2279/WebSite1/Default.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(" rel="nofollow noreferrer noopener"~/#") %>" runat="server" >here</a>

Control Code-Behind:

protected string URLHelper(string s)
{
    return Control.ResolveUrl(s);
}

Method 10

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x