Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
Hi all,

I have a table that contains a column of external website addresses.
I need to list those websites in a table, and I did it. But what I want is, when I click on this link, the control should be transferred to the specified website in a new tab.

Can anyone please help me!
Posted
Updated 23-Feb-22 6:14am
v2

Add target="_blank" to your anchor tags.

It will open the link in new tab (or new window if that is disabled)


If this helps please take time to accept the solution. Thank you.
 
Share this answer
 
Comments
Jineesh TR 12-Jan-15 2:35am    
Hi Sinisa,
When i click on the link it goes to new tab, but link prefixes the path of my website's current page from where the link is clicked. Can we avoid it or remove it
Sinisa Hajnal 12-Jan-15 3:48am    
Don't use MVC helper, just put anchor tag and absolute link to it (you can put the link from the model - same for link text)
In MVC, there is no helpers for generating links based on absolute URLS (i.e., to external resources). But you can write your own helper for that if you want the view to look nice with the consistent Helper formatting.

http://www.asp.net/mvc/tutorials/creating-custom-html-helpers-cs

example:
create extension method
C#
public static class LinkHelper
{
	public static string ExternalLink(this HtmlHelper helper, string url, string text)
        {
        	return String.Format("<a href='http://{0}' target="_blank">{1}</a>", url, text);
	}
}


and call this in your view like:

C#
@Html.ExternalLink("www.google.com", "Google")
 
Share this answer
 
v2
Comments
Jineesh TR 12-Jan-15 2:46am    
I searched there but I didnt get an answer. Can you please provide me some more data..
[no name] 12-Jan-15 3:32am    
@jineesh, Please check the answer, I updated the with code.
Jineesh TR 12-Jan-15 4:00am    
thanks a lot abhinav.. It really worked fine for me..
[no name] 12-Jan-15 5:41am    
welcome Jineesh

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900