[asp.net] how to open a page in new tab on button click in asp.net?

I want to open a page in new tab of browser on button click.

I have searched a lot on google but i couldn't find anything.

Here is my button.

   <asp:Button ID="btnNewEntry" runat="Server" CssClass="button" Text="New Entry" OnClick="btnNewEntry_Click" />

    protected void btnNewEntry_Click(object sender, EventArgs e)
    {      
        Response.Redirect("New.aspx");
    }

Can you please help me how i can do this ?

This question is related to asp.net

The answer is


add target='_blank' after check validation :

_x000D_
_x000D_
    <asp:button id="_ButPrint"  ValidationGroup="print" OnClientClick="if (Page_ClientValidate()){$('form').attr('target','_blank');}"  runat="server" onclick="ButPrint_Click" Text="print" />
                                                                 
_x000D_
_x000D_
_x000D_


You could do this on the ASPX HTML front end to make the button go to a new tab to show page in your ASP.NET site dynamically:

        <asp:Button ID="btnNewEntry" CssClass="button"  OnClientClick="window.open('https://website','_blank'); return false;" text="WebsiteName"  runat="server" />

Add this Script  
<script type = "text/javascript">
 function SetTarget() {
     document.forms[0].target = "_blank";
 }
</script>
and 
<asp:Button ID="BTNpRINT"  runat="server" Text="PRINT"  CssClass="btn btn-primary"  OnClick="BTNpRINT_Click" OnClientClick = "SetTarget();"/>    
and 
protected void BTNpRINT_Click(object sender, EventArgs e)
    {
        Response.Redirect(string.Format("~/Print.aspx?ID={0}",txtInv.Text));
    }

Why not just call window.open straight from OnClick?

<asp:Button ID="btnNewEntry" runat="Server" CssClass="button" Text="New Entry" OnClick="window.open('New.aspx')" />

Add_ supplier is name of the form

private void add_supplier_Load(object sender, EventArgs e)
{
    add_supplier childform = new add_supplier();
    childform.MdiParent = this;
    childform.Show();
}

You shuld do it by client side. you can place a html hyperlink with target="_blank" and style="display:none". after that create a javascript function like following

function openwindow(){
$("#hyperlinkid").click();
return false;
}

use this function as onclientclick event handler of the button like onclientclick="return openwindow()" You need to include a jquery in the page.


Just had the same problem. Client-side wasn't appropriate because the button was posting back information from a listview.

Saw same solution as Amaranth's on way2coding but this didn't work for me.

However, in the comments, someone posted a similar solution that does work

OnClientClick="document.getElementById('form1').target ='_blank';"

where form1 is the id of your asp.net form.


You have to use Javascript since code behind is server side only. I am pretty sure that this works.

<asp:Button ID="btnNewEntry" runat="Server" CssClass="button" Text="New Entry" OnClick="btnNewEntry_Click" OnClientClick="aspnetForm.target ='_blank';"/>

protected void btnNewEntry_Click(object sender, EventArgs e)
{
  Response.Redirect("New.aspx");
}

In vb.net either on button click or on link button click, this will work.

System.Web.UI.ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "openModal", "window.open('CertificatePrintViewAll.aspx' ,'_blank');", True)

Try This

<a href="#" target="_blank">Link</a>

A simple solution:

<a href="https://www.google.com" target="_blank">
    <button type="button">Open new tab</button>
</a>

Per Open a URL in a new tab (and not a new window) using JavaScript

Nothing an author can do can choose to open in a new tab instead of a new window.

The browser decides between opening a new tab or opening a new window. You cannot control this as a developer.


try this rather than redirect...

Response.Write("<script>");
Response.Write("window.open('ClickPicture.aspx','_blank')");
Response.Write("</script>");

You can add to your button OnClientClick like so:

<asp:Button ID="" runat="Server" Text="" OnClick="btnNewEntry_Click" OnClientClick="target ='_blank';"/>

This will change the current form's target for all buttons to open in new tab. So to complete the fix you can then use 2 approaches:

  1. For any other button in this form, add to client click a "reset form target" function like so:
function ResetTarget() {
   window.document.forms[0].target = '';
}
  1. Add the same code inside the function inside a setTimeout() so the code will reset the form's target after few moments. See this answer https://stackoverflow.com/a/40682253/8445364

Use JavaScript for the main form / Button click event. An example is:

Context.Response.Write("<script language='javascript'>window.open('AccountsStmt.aspx?showledger=" & sledgerGrp & "','_newtab');</script>")

Take care to reset target, otherwise all other calls like Response.Redirect will open in a new tab, which might be not what you want.

<asp:LinkButton OnClientClick="openInNewTab();" .../>

In javaScript:

<script type="text/javascript">
    function openInNewTab() {
        window.document.forms[0].target = '_blank'; 
        setTimeout(function () { window.document.forms[0].target = ''; }, 0);
    }
</script>