How do I specify that I want my hyperlink to always be absolute path?
I have this link of code where I populate a hyperlink with an address at runtime from the database.
<asp:HyperLink ID="HyperLink1" runat="server" Target="_blank"
NavigateUrl='<%#Eval("Source") %>'><%#Eval("Source") %></asp:HyperLink>
The problem is that it's treating the link as relative. so if the link is yahoo.com, it'll go to
http://localhost/yahoo.com
开发者_Go百科or something to that effect.
If my link source is http://www.yahoo.com, that will work, but I cannot guarantee that the links may or may not have the http:// at the start.
How can I get it to always treat it as an absolute URL.
You can try something like this
NavigateUrl='<%# GetUrl(Eval("Source")) %>'>
with
public string GetUrl(object source)
{
if(source != null)
{
string str = source.ToString();
return str.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase) ?
str :
string.Format("http://{0}",str);
}
return string.Empty;
}
精彩评论