In this article I will explain with an example, how to open new tab from Server-Side (Code-Behind) using
Response.Redirect,
Server.Transfer and
window.open in
ASP.Net using C# and VB.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.
HTML Markup
The following HTML Markup consists of:
Button – For redirecting to another page.
The Button has been assigned with an OnClick and OnClientClick event handlers.
<asp:Button ID="btnResponseRedirect" runat="server" Text="Response Redirect" OnClick="ResponseRedirect" OnClientClick="SetTarget();" />
<asp:Button ID="btnServerTransfer" runat="server" Text="Server Transfer" OnClick="ServerTransfer" OnClientClick="SetTarget();" />
<asp:Button ID="btnWindowOpen" runat="server" Text="Window Open" OnClick="WindowOpen" OnClientClick="document.forms[0].target = ''" />
Setting target property of FORM Tag
An HTML FORM tag has
target property which can be used to open a new window when the Form is submitted.
Note: But as per W3C this method target is not a valid property in XHTML 1.1 though it works with all major browsers.
When the Button is clicked, first the
SetTarget JavaScript function is executed inside which the
target property of the Form is set to
_blank.
<script type="text/javascript">
function SetTarget() {
document.forms[0].target = "_blank";
}
</script>
1. Response.Redirect
When the Button is clicked, the page is redirected to another Page with QueryString value using Response.Redirect.
C#
protected void ResponseRedirect(object sender, EventArgs e)
{
Response.Redirect("Page.aspx?id=1");
}
VB.Net
Protected Sub ResponseRedirect(ByVal sender As Object, ByVal e As System.EventArgs)
Response.Redirect("Page.aspx?id=1")
End Sub
2. Server.Transfer
When the Button is clicked, the page is redirected to another Page with QueryString value using Server.Transfer.
C#
protected void ServerTransfer(object sender, EventArgs e)
{
Server.Transfer("Page.aspx?id=2");
}
VB.Net
Protected Sub ServerTransfer(sender As Object, e As EventArgs)
Server.Transfer("Page.aspx?id=2")
End Sub
3. window.open
When the Button is clicked, the URL of the Page where it will be redirected with QueryString is set in window.open method.
Finally, the
JavaScript Alert Message Box will be displayed using
RegisterStartupScript method.
C#
protected void WindowOpen(object sender, EventArgs e)
{
string url = "Page.aspx?id=3";
StringBuilder sb = new StringBuilder();
sb.Append("window.open('");
sb.Append(url);
sb.Append("');");
ClientScript.RegisterStartupScript(this.GetType(), "script", sb.ToString(), true);
}
VB.Net
Protected Sub WindowOpen(sender As Object, e As EventArgs)
Dim url As String = "Page.aspx?id=3"
Dim sb As New StringBuilder()
sb.Append("window.open('")
sb.Append(url)
sb.Append("');")
ClientScript.RegisterStartupScript(Me.GetType(), "script", sb.ToString(), True)
End Sub
Displaying appropriate message using QueryString
HTML Markup
The following HTML Markup consists of:
Label – For displaying message.
<h2>Page<asp:Label ID="lblMessage" runat="server" /></h2>
Displaying message
Inside the Page_Load event handler, a switch case statement is executed.
Inside the switch case statement, the value of the Id is fetched from QueryString and the appropriate message is displayed according to matched case.
C#
protected void Page_Load(object sender, EventArgs e)
{
switch (Request.QueryString["id"])
{
case "1":
lblMessage.Text = "Opened using Response.Redirect";
break;
case "2":
lblMessage.Text = "Opened using Server.Transfer";
break;
case "3":
lblMessage.Text = "Opened usingWindow.Open";
break;
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Select Case Request.QueryString("id")
Case "1"
lblMessage.Text = "Opened using Response.Redirect"
Case "2"
lblMessage.Text = "Opened using Server.Transfer"
Case "3"
lblMessage.Text = "Opened usingWindow.Open"
End Select
End Sub
Screenshot
Browser Compatibility
* All browser logos displayed above are property of their respective owners.
Demo
Downloads