In this article I will explain with an example, how to use target _blank with
Response.Redirect function in
ASP.Net using C# and VB.Net.
By default,
Response.Redirect redirects Page to another Page in same Tab (Window) but with some tweaks, it can be used to open another Page in new Tab (Window) in
ASP.Net using C# and VB.Net.
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
Button – For redirecting to another page.
The Button has been assigned with an OnClick and OnClientClick event handlers.
When the
Button is clicked, first the SetTarget
JavaScript function is executed inside which the target property of the Form is set to _blank.
<form id="form1" runat="server">
<asp:Button ID="btnResponseRedirect" runat="server" Text="Response Redirect"
OnClick="ResponseRedirect" OnClientClick="SetTarget();" />
<script type="text/javascript">
function SetTarget() {
document.forms[0].target = "_blank";
}
</script>
</form>
Code
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("PageCS.aspx?id=1");
}
VB.Net
Protected Sub ResponseRedirect(sender As Object, e As EventArgs)
Response.Redirect("PageVB.aspx?id=1")
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 ServerTransfer(object sender, EventArgs e)
{
Server.Transfer("PageCS.aspx?id=2");
}
VB.Net
Protected Sub ServerTransfer(sender As Object, e As EventArgs)
Server.Transfer("PageVB.aspx?id=2")
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="btnWindowOpen" runat="server" Text="Window Open"
OnClick="WindowOpen" OnClientClick="document.forms[0].target = ''" />
</form>
Code
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 = "PageCS.aspx?id=3";
StringBuilder sb = new StringBuilder();
sb.Append("window.open('");
sb.Append(url);
sb.Append("');");
ClientScript.RegisterStartupScript(this.GetType(), "alert", "window.open('" + url + "');", true);
}
VB.Net
Protected Sub WindowOpen(sender As Object, e As EventArgs)
Dim url As String = "PageVB.aspx?id=3"
Dim sb As New StringBuilder()
sb.Append("window.open('")
sb.Append(url)
sb.Append("');")
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "window.open('" + url + "');", True)
End Sub
Browser Compatibility
* All browser logos displayed above are property of their respective owners.
Demo
Downloads