Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

ASP.NET MVC – Hyperlinks – Open the Page in a New Browser Window

0.00/5 (No votes)
3 Jun 2014 1  
ASP.NET MVC – Hyperlinks – Open the page in a new browser window

In the last blog post, we have discussed about DataType & DisplayColumn attributes in ASP.NET MVC. You can read that article here. In this article, we will look at how to open the page in a new browser window while clicking on a hyperlink.

Let’s understand this with an example. We will be using the same example which we have used in the previous article. First of all, change the Details action method in the Home controller like below.

public ActionResult Details(int id)
{
SampleDBContext db = new SampleDBContext();
tblEmployee employee = db.tblEmployees.Single(x => x.Id == id);
return View(employee);
}

<img alt="MVC1" class="alignnone wp-image-2426" src="781804/mvc110.png" style="width: 600px; height: 275px;" />

Then, change the Details View like below:

<img alt="MVC2" class="alignnone wp-image-2427" src="781804/mvc24.png" style="width: 600px; height: 173px;" />

Build the solution and run it. At this point, we should be able to see the employee’s full details.

<img alt="MVC3" class="alignnone wp-image-2428" src="781804/mvc34.png" style="width: 600px; height: 332px;" />

Notice that PersonalWebSite property is rendered as a hyperlink. It is because within the tblEmployee class, we have decorated the PersonalWebSite property with DataType attribute.

<img alt="MVC4" class="alignnone wp-image-2429" src="781804/mvc44.png" style="width: 600px; height: 323px;" />

Right click on the page and select View page source. Notice the anchor tag generated here. It has a href attribute, but doesn’t have a target attribute.

<img alt="MVC5" class="alignnone wp-image-2430" src="781804/mvc54.png" style="width: 600px; height: 247px;" />

We know that if we want a Url to open in a new window, the target attribute needs to be set to _blank. Since this anchor tag doesn’t have a target attribute, when we click on the hyperlink, the target page is going to open in the same window.

<img alt="MVC6" class="alignnone wp-image-2431" src="781804/mvc64.png" style="width: 600px; height: 202px;" />

Let’s see how to open the Url in a new window. Follow the below steps:

  • Right click on Views folder and add Shared folder.
  • Right click on Shared folder and add DisplayTemplates folder.
  • Right click on DisplayTemplates folder and add a View. Set Url as the name and use Razor view engine.

<img alt="MVC7" class="alignnone wp-image-2432" src="781804/mvc78.png" style="width: 600px; height: 404px;" />

Notice the name of the View here. It matches the enum value DataType.Url. Then copy and paste the following code in Url.cshtml view.

<a href=&rdquo;@ViewData.Model&rdquo; target=&rdquo;_blank&rdquo;>@ViewData.Model</a>

<img alt="MVC8" class="alignnone wp-image-2433" src="781804/mvc85.png" style="width: 600px; height: 60px;" />

Now build the solution and refresh the View. Let’s right click on the page and select View page source. Notice the anchor tag right here. Now we have a target attribute which is set to _blank.

<img alt="MVC9" class="alignnone wp-image-2434" src="781804/mvc94.png" style="width: 600px; height: 220px;" />

If we click on the link now, the Url should be opened in a new window.

<img alt="MVC10" class="alignnone wp-image-2435" src="781804/mvc104.png" style="width: 600px; height: 219px;" />

But the downside of this approach is that, from now onwards, all the links will be opened in a new window. But our requirement is such that only PersonalWebSite should be opened in a new window. Rest of the links within our application need to be opened in the same window. For this, 2 simple modifications have to be done.

  1. Rename Url.cshtml to OpenInNewWindow.cshtml
  2. Decorate PersonalWebSite property in EmployeeMetaData class with UIHint attribute and specify the name of the template to use. In our case, the name of the template is OpenInNewWindow.
public class EmployeeMetaData
{
    [DataType(DataType.Url)]
    [UIHint("OpenInNewWindow")]
    public string PersonalWebSite { get; set; }
}

<img alt="MVC11" class="alignnone wp-image-2438" src="781804/mvc11.png" style="width: 600px; height: 351px;" />

Now run the application and we can see that except the PersonalWebSite, all the other links will be opened in the same window.

From the above example itself, it is clear that UIHint attribute is used to specify the name of the template to use to display the data field.

Reference

Arun Ramachandran (http://BestTEchnologyBlog.Com)

<img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/blogdotbesttechnologydotcom.wordpress.com/2424/" /> <img alt="" border="0" height="1" src="http://stats.wordpress.com/b.gif?host=besttechnologyblog.com&blog=58485837&post=2424&subd=blogdotbesttechnologydotcom&ref=&feed=1" width="1" />

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here