Few days back there was a question on ASP.Net forums regarding how to use Respose.Redirect or Server.Transfer to open a new window. The fact is directly both these methods will not open your page in new window hence you will need to use the following tricks to achieve the same
1. Setting target property of FORM
HTML FORM tag has target property which can be used to open a new window when the form is submitted to the server. For more information read here
HtmlForm.Target Property
Thus with the help of JavaScript we can set the target property of form to _blank whenever we want to open the page in a new window. Refer below
I have an ASP.Net Button
<asp:Button ID="Button1" runat="server" Text="Button"
onclick="Button1_Click" OnClientClick = "SetTarget();" />
I am calling the SetTarget() JavaScript function OnClientClick event of the ASP.Net Button Control as described below
<script type = "text/javascript">
function SetTarget() {
document.forms[0].target = "_blank";
}
</script>
As you can see above I am setting the target property of the HTML FORM to _blank
Secondly I am calling the Button1_Click method onclick event Control as described below
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
Thus now when you click on the Button, the JavaScript function sets the target property of the form is to _blank Thus when PostBack occurs it first opens the current page in new window and then executes the Response.Redirect method thus helping us achieve our goal.
Same way we can use Server.Transfer on the Button Click event
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
The only difference will be that both the windows parent and child will display the same URL though displaying different pages
But as per W3C this method target is not a valid property in XHTML 1.1 though it works with all major browsers.
Hence there’s another way to Redirect to a new window that is using JavaScript window.open
2. JavaScript window.open
Using ClientScript we can open a new window from server side in the following way
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 Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.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
Thus this is how you can open a new window using JavaScript from Server side. Ultimately if you compare both the techniques do same thing that is open a new window.