Response.Redirect Open Page In New Tab


I am going to explain how to open a page in a new tab/window on Button click in asp.net. Suppose the page contains a button called "btnTest" and your requirement is open a specified page in the new tab on the button click then do the following.

<asp:Button ID=”btnTest” runat=”Server” Text=”Test” OnClick=”btnTest_Click”
OnClientClick ="document.forms[0].target = '_blank';"/>

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

OR

If there is some code to execute on button click and after that you want to open the page in new tab then do the following :

protected void btnTest_Click(object sender, EventArgs e)
{
      .
      .         // Your Code Here
      .
      Response.Write( “<script> window.open( ‘New.aspx’,'_blank' ); </script>”);
      Response.End();
}

OR

If you want to open the page in a new tab as well as pass some values to the specified page using querystring then do the following :

protected void btnTest_Click(object sender, EventArgs e)
{
      .
      .
      .
      string pageurl="Default.aspx?Id=6&RollNo=15";

      Response.Write( “<script> window.open( ‘"+pageurl+"’,'_blank' ); </script>”);
      Response.End();
}

Comments