4

I have this linkbutton here...

<asp:LinkButton ID="linkButton" CssClass="Button" runat="server" target="_blank">Button Text</asp:LinkButton>

but the target blank does not work, it does not open the page in a new tab, it opens it in the same tab.

What Am I doing wrong?

There is an href, its gets assigned in the code behind like so

 linkButton.PostBackUrl = "http://www.nfl.com";

but still the target blank does not work....

user979331
  • 11,039
  • 73
  • 223
  • 418

3 Answers3

4

You can try this. Hope it helps:

<asp:LinkButton ID="linkButton" OnClientClick="window.document.forms[0].target='_blank';" runat="server">Button Text</asp:LinkButton>

linkButton.PostBackUrl = "http://www.nfl.com";
McKeymayker
  • 358
  • 3
  • 12
  • This works fine, but if you go back to the original page and open any other link, then it is "target=_blank" by default – deebs Jul 06 '15 at 14:53
1

Below code works fine:

OnClientClick="window.document.forms[0].target='_blank';

0

In ASP.NET web forms a button, linkbutton, imagebutton or similar control actually just submits the underlying form. In order to open this in a new window you can modify the target property of the "form" using JavaScript. We also need to undo the modification after the click otherwise further button clicks will unintentionally target the new tab, which we can do using a setTimeout and blanking the target again.

<asp:LinkButton ID="uiNewTabExample" Text="PDF" OnClick="uiNewTabExample_Click" OnClientClick="window.document.forms[0].target = '_blank'; setTimeout(function () { window.document.forms[0].target = '' }, 0);" 
runat="server" />
William
  • 8,007
  • 5
  • 39
  • 43