views:

922

answers:

7

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

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

A: 

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

<a href="#" >Your Link Text/Image Here</a>
CmdrTallen
Removing the runat="server" is not an option as other attributes are changed in the backend. This was just a simplification.
Highstead
A: 
ChaosPandion
To be clear, this answer will *not* provide what is specifically asked for...(the '#' character), but may well be good enough if all the OP needs to do is have a link that "does nothing."
Beska
The Example was such to simplify the problem. I'm just syntactically looking to follow the href="#". The anchor tag has a few other attributes.
Highstead
+1  A: 

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", "#");
Brendan Kowitz
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). I suppose a true fix would be to get microsoft to make UrlPath.IsRelativeUrl() smarter.
Sorpigal
A: 

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"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<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);
}
rick schott
Control is in path Project/userControls/myControl.ascx Page is in path Project/CRUD/page.aspxIt was in a repeater but the functionality is the same outside the repeater
Highstead
Crap, forgot about the nested path, I will test that out.
rick schott
A: 

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="#" 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...

Manish
The control is in a seperate folder. Specifically project/UserControls/myControl.ascx. The page is in the folder project/Crud/MyPage.aspx. I'm able to duplicate this functionality every time.
Highstead
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.

Highstead
A: 

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.

James