In this post, I will show you how to open a new window in the browser using Response.Redirect
asp.net. Check the following code it’s self-explanatory. If you have any issue comments in the comment box.
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="RedirectToAnotherPage.aspx.cs" Inherits="RedirectToAnotherPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Main Window</h1>
<asp:Button ID="btn" runat="server" Text="Redirect" OnClick="btn_Click" />
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class RedirectToAnotherPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public static class ResponseHelper
{
public static void Redirect(string url, string target, string windowFeatures)
{
HttpContext context = HttpContext.Current;
if ((String.IsNullOrEmpty(target) ||
target.Equals("_self", StringComparison.OrdinalIgnoreCase)) &&
String.IsNullOrEmpty(windowFeatures))
{
context.Response.Redirect(url);
}
else
{
Page page = (Page)context.Handler;
if (page == null)
{
throw new InvalidOperationException(
"Cannot redirect to new window outside Page context.");
}
url = page.ResolveClientUrl(url);
string script;
if (!String.IsNullOrEmpty(windowFeatures))
{
script = @"window.open(""{0}"", ""{1}"", ""{2}"");";
}
else
{
script = @"window.open(""{0}"", ""{1}"");";
}
script = String.Format(script, url, target, windowFeatures);
ScriptManager.RegisterStartupScript(page,
typeof(Page),
"Redirect",
script,
true);
}
}
}
protected void btn_Click(object sender, EventArgs e)
{
ResponseHelper.Redirect("popup.aspx", "_blank", "menubar=0,width=100,height=100");
}
}