Redirecting to new window in c#

As a developer, many time I wished to redirect user to a new URL into a new browser window। I wanted to use response।redirect at server side and open the new web page into a new window। But the way redirection works makes it impossible to open a new window from server side. The common solution to this problem is to use java script’s statement at client side and then fetch the new URL into the new window. To me, this is totally unacceptable while I am writing server side code. So I decided to write some code to do this. But before writing, I googled for the same.I found a good solution for the problem. I would like to give proper credit the author, who choose not to publish his name. As they say a code worth one thousand words, here is the code

ResponseHelper Class:

The ResponseHelper class does all the magic. It adds a client side script to the response which opens the new window. Use this class in place of response class if you want to open new window.

public static class ResponseHelper{
public static void Redirect(string url, string target, string windowFeatures) {
HttpContext context = HttpContext.Current;
target.Equals("_self", StringComparison.OrdinalIgnoreCase))
&& String.IsNullOrEmpty(windowFeatures)) {
Pagepage = (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 = @"""{0}"", ""{1}"", ""{2}"");";
script = @"""{0}"", ""{1}"");";
script = String.Format(script, url, target, windowFeatures);
ScriptManager.RegisterStartupScript(page, typeof(Page), "Redirect", script, true);

Using Response Helper Class:

ResponseHelper class’ static Redirect method is used to redirect into new

window.ResponseHelper.Redirect(“default.aspx”, “_blank”, null);

The Rediect method accepts thee parameters:

  1. Redirection URL: The URl which will be opened into the new window.
  2. Target Frame:

    1. Use “_Self”, if you don’t want to open new window. Null means “_Self”.
    2. Use “_Blank” to open into new window.
  3. Window Feature: Window features can be added like width, height, scrolling etc. using this parameter.


  1. If you use it outside the context of a Page request, you can’t redirect to a new window. The reason is the need to call the ResolveClientUrl method on Page, which I can’t do if there is no Page. I could have just built my own version of that method, but it’s more involved than you might think to do it right. So if you need to use this from an HttpHandler other than a Page, you are on your own.
  2. Beware of popup blockers. New window is a popup. So it will not work if popup is blocked.
  3. Obviously when you are redirecting to a new window, the current window will still be hanging around. Normally redirects abort the current request — no further processing occurs. But for these redirects, processing continues, since we still have to serve the response for the current window (which also happens to contain the script to open the new window, so it is important that it completes).

2 thoughts on “Redirecting to new window in c#

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s