Friday, February 13, 2009

asp:HyperLink NavigateUrl Problem Absolute URL

I came across an idiotic problem with asp:HyperLink control.
What happened is that I noticed every time I bind a hyperlink NavigateURL field in code-behind, it points to current site + the url i specifed.

Example:

In Page_Load i do:

lnkAbsolute.NavigateURL = "http://www.google"

But the result on the page is

http://www.mysite.com/www.google.com

IDIOTIC


Solution

So the solution i found is not relay good but nevertheless a solution.

I saw that in Page_PreRened event my NavigateURL lost "http://" part of the URL
so you just have to re-add the "http://" prefix to the URL in Page_PreRender event, like so:

protected void Page_PreRender(object sender, EventArgs e)
{
if (!lnkAbsolute.NavigateUrl.StartsWith("http://") && !string.IsNullOrEmpty(lnkAbsolute.NavigateUrl))
{
lnkAbsolute.NavigateUrl = "http://" + lnkAbsolute.NavigateUrl;
}
}


Sometimes I just can believe how the simplest of things can hold you back.

2 comments:

Mohammad Nazmi said...

thanks man, yea it was frustraiting me too, but to be Honest I dunno if this is MicroSoft shit, or inadequacy in you and me, we must know that if a link doesn’t start with http:// then it wont be considered or transferred using a (hyper text transfer protocol) which is a protocol that internet browsers append by default if you supply a link like “mysite.com”, what I did to solve my problem which was exactly your problem (“After reading your post ) is that in my insert/update code for the dynamic links, I made sure I append “http://” at the beginning of the url, and simply did nothing else for the asp:hyperlink control

Madhu said...

Thank you dude... it really helps.