15

No javascript/AJAX to be used.

when clicked on the hyperlink, it should open a new browser window.

4 Answers 4

36

Basic HTML Anchor Element:

<a href="http://www.w3schools.com/"
target="_blank">Visit W3Schools!</a>

ASP.NET WebForms HyperLink Element:

<asp:HyperLink ID="HyperLink1" runat="server" Target="_blank">HyperLink</asp:HyperLink>

ASP.NET MVC Style:

<%= Html.ActionLink<HomeController>(c => c.Index(), "Click me", new { target = "_blank" }) %>

All three open a new tab, would that suit your needs?

2
  • This is true but I just wanted to add you do not want to use the asp:hyperlink in asp.net mvc see the below answer by James S.
    – David
    Aug 29, 2009 at 2:01
  • Of course, it can be done with the Html.ActionLink item too. - altered my comment to match it.
    – Faizan S.
    Aug 29, 2009 at 14:40
14

If you're not using javascript, you need to use the target="_blank". But to do it in a cleaner mvc fashion, do:

<%= Html.ActionLink("Click me", "ActionName", null, new {target="_blank"}) %>
4
  • Just how is that cleaner than the following <a href="ActionName" target="_blank">Click Me</a> Aug 29, 2009 at 0:36
  • It's a little weird but it's the asp.net mvc way, and you can do things create your own helpers etc., also you can easily put in the view vars here, instead of hard coding the strings.
    – David
    Aug 29, 2009 at 2:03
  • 3
    Yes. It's weird. And, no, it's not cleaner. However, ActionLink "knows" about your routes. So if your routes change, then the link gets updated. Also, after you've put your view vars in there, it'll keep track of variable names, so refactoring is easier, and also strip out parameters with null values (which is pretty cool). If you strongly type it, it's even better for compile-time checking, but it's a bit funkier looking (<%= Html.ActionLink<ClickController>(c => c.Action(var1, null, var3), "Click Me", new {target="_blank"}); %>).
    – James S
    Aug 29, 2009 at 9:17
  • 1
    Oh. And visual studio has problems with variable names in quotes in HTML, so it won't autocomplete on something like <a href="<%= c.ID %>"...>. I used to hardcode all my hrefs manually, but I've switched to strongly typed.
    – James S
    Aug 29, 2009 at 9:20
5

If your question is - How can I create pop-up window in asp.net mvc

The simple answer is : can't

For that matter you can't in PHP, JSP or any other server side scripting language.

You noticed that the solutions above are all HTML?

The pop-up window is a domain that has to be handled client side. The server languages can spew HTML/Javsascript that have the commands to open a pop-up window. They intrinsically can't order the browser to open a window.

3
<A Href="page.html" target="_blank">Link text </A>

The target="_blank" is the specific part you need.

Alternatively you could use target="new". Here's an article that describes how the two behave differently.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.