Opening a new Windows from ASP.NET code behind

Posted by TATWORTH on Geeks with Blogs See other posts from Geeks with Blogs or by TATWORTH
Published on Fri, 24 Dec 2010 20:31:49 GMT Indexed on 2010/12/24 20:54 UTC
Read the original article Hit count: 349

Filed under:

At http://weblogs.asp.net/infinitiesloop/archive/2007/09/25/response-redirect-into-a-new-window-with-extension-methods.aspx there is an excellent post on how to open a new windows from code behind.

The purists may not like it but it helped solve a problem for a client's client.

Here is an update for VS2010 users:

using System;
using System.Web;
using System.Web.UI;

/// <summary>
/// Response Helper for opening popup windo from code behind.
/// </summary>
public static class ResponseHelper
{
  /// <summary>
  /// Redirect to popup window
  /// </summary>
  /// <param name="response">The response.</param>
  /// <param name="url">URL to open to</param>
  /// <param name="target">Target of window _self or _blank</param>
  /// <param name="windowFeatures">Features such as window bar</param>
  /// <remarks>
  ///     <list type="bullet">
  ///         <item>
  /// From http://weblogs.asp.net/infinitiesloop/archive/2007/09/25/response-redirect-into-a-new-window-with-extension-methods.aspx
  /// </item>
  /// <item>
  /// Note: 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.
  /// </item>
  ///         <item>
  /// Beware of popup blockers.
  /// </item>
  /// <item>
  /// Note: 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).
  /// </item>
  /// <item>
  /// Sample call Response.Redirect("popup.aspx", "_blank", "menubar=0,width=100,height=100");
  /// </item>
  ///     </list>
  /// </remarks>
  public static void Redirect(this HttpResponse response, string url, string target, string windowFeatures)
  {
    if ((String.IsNullOrEmpty(target) || target.Equals("_self", StringComparison.OrdinalIgnoreCase)) && String.IsNullOrEmpty(windowFeatures))
    {
      response.Redirect(url);
    }

    else
    {
      Page page = (Page)HttpContext.Current.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);
    }
  }
}

 

© Geeks with Blogs or respective owner