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("~/#") %>" runat="server" >here</a>
Control Code-Behind:
protected string URLHelper(string s)
{
return Control.ResolveUrl(s);
}
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>
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.
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", "#");