Showing posts with label HyperLink. Show all posts
Showing posts with label HyperLink. Show all posts

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.