Ran into a strange bug today while trying to make an anchor link inside a user control i ASP.NET. The obvious way to do it would be to just to put the anchor ID inside the NavigateUrl attribute of the HyperLink control like NavigateUrl=”#anchor”. That however resulted in the link and anchor pointing to the directory where the user control was located.
So I tried to put the application root modifier in front of the anchor like NavigateUrl=”~/#anchor”. That resulted in the anchor being printed twice in the resulting link, Default.aspx#anchor#anchor. This must be a bug for I can’t imagine any sane use for this kind of behavior.
The solution I came up with was to construct the anchor link manually in my code behind file.
April 8th, 2008
by Gregory McKenzie
Try adding a attribute instead of populating the .NavigateUrl property. For example:
if (objAlert.Url.Substring(0, 1) == “#”)
objHyperLink.Attributes.Add(“onClick”, “Javascript:location.href(‘” + objAlert.Url + “‘);”);
else
objHyperLink.NavigateUrl = objAlert.Url;
July 8th, 2008
by Barbaros Alp
I had the same problem today, after reading the comment i ve found a great solution without using CodeBehind.
Use asp.net control and add attribute..
Go There
if there is no NavigateUrl defined in the HyperLinkField the cursor looks like there is no link under that button you will see just the cursor. For this i defined a little css
.linkButtons
{
cursor:pointer;
}
Thanks, hope this helps
September 10th, 2008
by Jon Sagara
Instead of NavigateUrl=”#myanchor”, change it to href=”#myanchor”. Works just as well.
September 11th, 2008
by admin
The Author
Jon Sagara: Well how about that … the simplest thing!
June 12th, 2009
by darko
@Jon Sagara : it didn’t work to me. Instead I’ve used
listlink.HRef = Request.Url.PathAndQuery + “#”;