1

I need to open an aspx page in a new tab, I searched differents solutions, but I've only found codes that open pop-ups and the browser blocks. I need to do it in the code behind

I hope somebody help me, THANKS.

Randall Sandoval
  • 237
  • 1
  • 6
  • 21
  • the link that you post is about a hyperlink, i need it in code behind – Randall Sandoval Jul 11 '14 at 16:50
  • I would change my close vote from "duplicate" to "'why won't it work' questions need code" if I could. You need to show what you have done so far. Show us the code that is giving you problems. – gunr2171 Jul 11 '14 at 16:52

2 Answers2

5

You cannot open a new page from code behind. Instead, you need to open at client side.

protected void Button1_Click(object sender, EventArgs e)
{
    string url = "http://www.stackoverflow.com";
    string script = string.Format("window.open('{0}');", url);

    Page.ClientScript.RegisterStartupScript(this.GetType(), 
        "newPage" + UniqueID, script, true);

    /* Use this if post back is via Ajax
    ScriptManager.RegisterStartupScript(Page, Page.GetType(), 
        "newPage" + UniqueID, script, true); */
}

The problem of opening at client side is sometime pop-up blocker blocks the page.

enter image description here

Win
  • 61,100
  • 13
  • 102
  • 181
1

The short answer is: you can't.

You especially can't do it from the server — there, you have zero access to browser functionality.

And on the client side of thing, you have very little direct access to browser functionality outside your own "window".

The best you can do is open a new window with a target window name of _blank. You can do that with HTML, thus:

<a href="http://some-url" target="_blank" >Link Text</a>

When the user clicks the link a new window should open (but, see below).

You can do it with javascript, thus:

window.open( "http://some-url", "_blank", "comma,delimited,list,of,window,features" ) ;

What happens then is entirely dependent on the browser and how the user has configured it:

  • It might not open at all (is a pop-up blocker running?),
  • It might open a new tab,
  • It might open a new window

One might note, too that a straight link without a window name might open in a new tab or window, too. For instance, I have my Firefox configured to open off-domain links in a new tab by default, and to open all target='_blank' links in a new tab rather than a new window.

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135