Blog Stats
  • Posts - 1288
  • Articles - 1
  • Comments - 127
  • Trackbacks - 0

 

Opening a new Windows from ASP.NET code behind

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);
    }
  }
}

 


Feedback

# re: Opening a new Windows from ASP.NET code behind

Gravatar Thanks so much for the post. 12/6/2011 10:49 AM | Brena Monteiro

# re: Opening a new Windows from ASP.NET code behind

Gravatar You can just write this simple code on form load or page load event.

linkbutton.Attributes.Add("onclick", "window.open('http://localhost:50/mailconfirm.htm','NameOfTheWindow','height=400,width=400,status=yes,toolbar=no,menubar=no,location=0,scrollbars=no,left=200,top=200')")

Here we are using the the ATTRIBUTES of a link button control and adding a "Click" event which opens a new window (pop up). That's it.

Arun

http://www.encodedna.com


11/28/2012 9:54 AM | arun

Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 
 

 

 

Copyright © TATWORTH