vyclarks
vyclarks

Reputation: 878

Cannot Navigate Hyperlink to outside of website

I have two simple aspx pages

Viewpage.aspx

<asp:HyperLink ID="lbllink" runat="server" Target="_Blank"></asp:HyperLink>

Editpage.aspx

<asp:TextBox ID="txtlink" runat="server"></asp:TextBox>// client input the link : faceboook.com

and in code behind:

lbllink.Text= txtlink.Text;// txtlink.Text = facebook.com
lbllink.NavigateUrl = txtlink.Text;

When I built, an error occur: The resource cannot be found. because facebook.com is not a page in my webstie folder.

I cannot use <a href= > because it doesnt have ID tag.

help, is there any better way for my problem???

Upvotes: 0

Views: 308

Answers (1)

jenson-button-event
jenson-button-event

Reputation: 18941

You cannot guess if its a relative or absolute url.

If you know it will always be an external url then you need to ensure the entered url has the http:// prepended else your server will assume its relative to the page it was clicked from.

The Uri Class has some pretty useful stuff.

You might find this method useful

public static string ToAbsoluteUrl(string relativeUrl) {
    if (string.IsNullOrEmpty(relativeUrl)) return relativeUrl;
    if(relativeUrl.ToLower().StartsWith("http")) return relativeUrl;

    return String.Format("http://{0}", relativeUrl);
}

Upvotes: 1

Related Questions