In this article I will explain with an example, how to open page in new Tab from code behind when using Response.Redirect or Server.Transfer in ASP.Net.
To open a page in new Tab is a browser property and newer browsers will automatically open pages on new tab using this code but older browsers will still open it in new window unless the default settings are changed.
1. Setting target property of FORM Tag
HTML FORM tag has target property which can be used to open a new window when the form is submitted to the server.
Note:But as per W3C this method target is not a valid property in XHTML 1.1 though it works with all major browsers.
HTML Markup
The following HTML Markup consists of a Form with an ASP.Net Button. The Button has been assigned with an OnClick event handler and also an OnClientClick event handler.
When the Button is clicked, first the SetTarget JavaScript function is called inside which the target property of the Form is set to _blank and after that the Server Side function is executed.
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" OnClientClick="SetTarget();" />
<script type="text/javascript">
function SetTarget() {
document.forms[0].target = "_blank";
}
</script>
</form>
Code
Inside the Button Click event handler, the Response.Redirect code is executed and the Page opens in new Tab.
C#
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Page2.aspx");
}
VB.Net
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Response.Redirect("Page2.aspx")
End Sub
In similar way, Server.Transfer function can be used to open Page in new Tab. The only difference will be that both the windows parent and child will display the same URL though displaying different pages.
C#
protected void Button1_Click(object sender, EventArgs e)
{
Server.Transfer("Page2.aspx");
}
VB.Net
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Server.Transfer("Page2.aspx")
End Sub
 
2. JavaScript window.open function to redirect to new Page in new Tab
HTML Markup
The following HTML Markup consists of a Form with an ASP.Net Button. The Button has been assigned with an OnClick event handler.
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button2_Click" />
</form>
Code
Inside the Button Click event handler, using the StringBuilder class, a JavaScript code is generated to open page in new Tab.
Finally, the script is registered using ClientScript.RegisterStartupScript method.
C#
protected void Button2_Click(object sender, EventArgs e)
{
string url = "Page2.aspx";
StringBuilder sb = new StringBuilder();
sb.Append("<script type = 'text/javascript'>");
sb.Append("window.open('");
sb.Append(url);
sb.Append("');");
sb.Append("</script>");
ClientScript.RegisterStartupScript(this.GetType(),
"script", sb.ToString());
}
VB.Net
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim url As String = "Page2.aspx"
Dim sb As New StringBuilder()
sb.Append("<script type = 'text/javascript'>")
sb.Append("window.open('")
sb.Append(url)
sb.Append("');")
sb.Append("</script>")
ClientScript.RegisterStartupScript(Me.GetType(), _
"script", sb.ToString())
End Sub
 
Demo
Downloads