0

I have a button on click event, and I want when the user click it to open another page but from the same problem.

I tried this

Response.Write("<script>window.open('http://www.google.com.hk/','_blank');</script>");

but the problem is that it opens the page not in a new tab

How can I open it in a new tab?

Suhaib Janjua
  • 3,538
  • 16
  • 59
  • 73
Marco Dinatsoli
  • 10,322
  • 37
  • 139
  • 253
  • 1
    If you find yourself using `Response.Write` to inject code or markup into a webpage, you're doing it wrong. There's always a better way. – mason Apr 06 '15 at 13:23

2 Answers2

2

Try this

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "key", "window.open('http://www.google.com.hk/','_blank');", true);
Mairaj Ahmad
  • 14,434
  • 2
  • 26
  • 40
1

I would prefer to use RegisterStartupScript instead of RegisterClientScriptBlock.

Given below is the way to use RegisterStartupScript. You should use it in your case.

ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "ReportPage", "javascript:window.open('EmailTemplate.aspx?emailAddress="+emailAddress+"','_blank');", true);

Because when you use RegisterStartupScript, it will render your script after all the elements in the page (right before the form's end tag). This enables the script to call or reference page elements without the possibility of it not finding them in the Page's DOM.

On the other hand when you use RegisterClientScriptBlock, the script is rendered right after the Viewstate tag, but before any of the page elements. Since this is a direct script (not a function that can be called, it will immediately be executed by the browser. But the browser does not find the label in the Page's DOM at this stage and hence you should receive an "Object not found" error.

For further details visit this answer.

Community
  • 1
  • 1
Suhaib Janjua
  • 3,538
  • 16
  • 59
  • 73