32

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 ?

punter
  • 460
  • 1
  • 6
  • 22
Rajbir Singh
  • 1,641
  • 6
  • 23
  • 46

18 Answers18

45

You could use window.open. Like this:

protected void btnNewEntry_Click(object sender, EventArgs e)
{ 
   Page.ClientScript.RegisterStartupScript(
   this.GetType(),"OpenWindow","window.open('YourURL','_newtab');",true);
}
Arion
  • 31,011
  • 10
  • 70
  • 88
8

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')" />
James Cahours
  • 85
  • 2
  • 8
  • What if I'm generating the url to open dynamically in the event handler for the button thats being clicked? – Ortund Jul 18 '19 at 09:25
5

Try This

<a href="#" target="_blank">Link</a>
Arion
  • 31,011
  • 10
  • 70
  • 88
Mahmoude Elghandour
  • 2,921
  • 1
  • 22
  • 27
5

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>
Nina
  • 1,035
  • 12
  • 17
4

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>")
Bryan
  • 2,870
  • 24
  • 39
  • 44
Pankaj
  • 41
  • 1
3

try this rather than redirect...

Response.Write("<script>");
Response.Write("window.open('ClickPicture.aspx','_blank')");
Response.Write("</script>");
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
2

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.

andyb
  • 770
  • 5
  • 11
1

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");
}
Amaranth
  • 2,433
  • 6
  • 36
  • 58
1
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));
    }
1

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
S.vaysrub
  • 19
  • 3
0

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.

Community
  • 1
  • 1
user247702
  • 23,641
  • 15
  • 110
  • 157
0

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)
JoSSte
  • 2,953
  • 6
  • 34
  • 54
0

add target='_blank' after check validation :

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

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" />
Chris Singleton
  • 157
  • 1
  • 1
  • 12
0

If the url is dynamic and you want to control it in the code behind

<asp:Button ID="btnNewEntry" runat="Server" Text="New Entry" />

//code behind
var id = 0;
btnNewEntry.OnClientClick = $"window.open('New.aspx?ID={id}')";
Michael
  • 2,825
  • 3
  • 24
  • 30
-1

A simple solution:

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

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.

Krishanu Dey
  • 6,326
  • 7
  • 51
  • 69
-4

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();
}
Storm
  • 684
  • 9
  • 20