Tuesday, September 25, 2007 11:01 AM InfinitiesLoop

Response.Redirect into a new window (with Extension Methods)

This question comes up from time to time, to time. If you understand how redirects work, then you also know it is "not possible" to redirect into a new window, because a redirect on the server causes a special HTTP response to be sent to the users browser, the client. The browsers native implementation interprets the special response code and sends the user off to the destination. There's no built-in mechanism or standard for specifying a new window.

The only way to open a new window is for it to be initiated on the client side, whether it be through script or clicking on a link.

So the solution always proposed to this problem is to instead write out some script that opens the window, rather than using Response.Redirect:

<script type="text/javascript">
    window.open("foo.aspx");
</script>

Ok... so first you lecture me about how it is "not possible", and then you give me the code that makes it possible. Why can't I just redirect to a new window -- I don't care how HTTP works or client this or server that. There's obviously a solution, so why do I have to worry about it?

(The make-believe developers in my head are always quite temperamental)

It's easy enough to write a little helper that abstracts the details away from us... while we're at it, we might as well add 'target' and 'windowFeatures' parameters. If we're going to open the new window with script, why not let you use all of the window.open parameters? For example, with 'windowFeatures' you can specify whether the new window should have a menu bar, and what its width and height are.

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

Now you just call ResponseHelper.Redirect, and it figures out how to honor your wishes. If you don't specify a target or you specify the target to be "_self", then you must mean to redirect within the current window, so a regular Response.Redirect occurs. If you specify a different target, like "_blank", or if you specify window features, then you want to redirect to a new window, and we write out the appropriate script.

One nice side effect of this "do you really need a new window?" detection is that it's dynamic. Say the destination you redirect to is configurable by some administrator. Now they can decide whether it opens in a new window or not. If they don't want it to they can specify blank or _self as the target.

Disclaimers:

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.

Note: Beware of popup blockers.

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).

Extension Methods

Recently, Eilon and Bertrand blogged about a novel use of some C# 3.0 features. Eilon posed the question, "Have you come up with a novel way to use a new language feature that you'd like to share?". Well here you go.

Extension Methods are a new feature in C# 3.0 (you'll need it for the rest of the article). They allow you to add methods to existing types, imported via a 'using' statement. I've seen a lot of debate over their use -- whether they are bad or good. Well -- I don't know, I don't really want to be involved in that debate. But I do know that in some scenarios they seem to fit perfectly. Like all language tools, you should use it sparingly and only when appropriate. I believe even the dreaded GOTO statement, which yes, exists in C#, has its place (I wasn't a believer originally, but some old coworkers of mine convinced me (Bob!)).

In this case, an extension method seems to work well. In general, whenever you find yourself writing a static Helper class whose only purpose in life is to help use the APIs of another type, it's probably a great candidate for extension methods. Especially if the first parameter to all those methods is the type you're trying to help with -- or if the methods always grabs the instance through some static API (like HttpContext.Current) or instantiates a new one.

By rewriting our ResponseHelper to use extension methods...

public static class ResponseHelper {
    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);
        }
    }
}

Note the 'this' keyword in the first parameter. Now whenever we include the namespace this class is defined within, we get a nice override on the actual Response object.

ResponseRedirect

Simply including a 'using' to a namespace is what gets extensions methods to show up. So it's probably a good idea to keep extension methods isolated to their own namespaces, lest someone get more than they bargained for when they use your namespace.

Also worth noting is that this is still a static API, so you can use it the traditional way, too. You just have to pass in the Response object as the first parameter.

And to see it in action...

Response.Redirect("popup.aspx", "_blank", "menubar=0,width=100,height=100");

Redirected into a new Window...

Filed under: , ,

Comments

# re: Response.Redirect into a new window (with Extension Methods)

Tuesday, September 25, 2007 2:57 PM by Joe Chung

Overloading Response.Redirect to display a popup is wrong on so many levels that it is not even funny.

If you want to make an API to display a popup from server-side code, fine, but don't pollute the API with your code by overloading something that is intended to be an encapsulation of an HTTP response, not a JavaScript code injector.

# re: Response.Redirect into a new window (with Extension Methods)

Tuesday, September 25, 2007 3:19 PM by InfinitiesLoop

Joe -- fair enough. From pure the perspective of what HttpResponse encapsulates, it is out of place. But from the perspective of a page developer, Response by itself doesn't always give you everything you want. Don't like polluting it.. then don't include the namespace, or simply remove 'this' from the parameter list, and its no longer an extention. Actually, perhaps this would be a better extention to the Page class instead of HttpResponse. That would more naturally imply the requirement for a Page instance. Yeah, makes more sense that way :)

# Response.Redirect into a new window (with Extension Methods)

Tuesday, September 25, 2007 4:18 PM by DotNetKicks.com

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# re: Response.Redirect into a new window (with Extension Methods)

Tuesday, September 25, 2007 4:19 PM by Derek

Can someone convert this to VB.NET?

# Extended Response.Redirect

Tuesday, September 25, 2007 6:00 PM by Same Old Applications

With new C# 3.0 feature Extension methods we can extending existing CLR types without sub-classing or

# re: Response.Redirect into a new window (with Extension Methods)

Tuesday, September 25, 2007 8:26 PM by Andy White

>url = page.ResolveClientUrl(url);

Dave,

Why are you calling this method? Some kind of security check?

Thanks,

Andy

# re: Response.Redirect into a new window (with Extension Methods)

Tuesday, September 25, 2007 9:34 PM by Glen Chen

Cool idea, I think it is a good example of extension methods! However, it may be a little bit confusing to read a code with a lot of extension methods for the framework.  

# re: Response.Redirect into a new window (with Extension Methods)

Tuesday, September 25, 2007 11:49 PM by Dave

hmmm. Seems like you want to emit this as well

<noscript>

<a href="popup.aspx" target="_blank">Have you considered enabling javascript? Click here to manually open a new window.</a>

</noscript>

# re: Response.Redirect into a new window (with Extension Methods)

Tuesday, September 25, 2007 11:56 PM by hkhaled

hey Derek here you go for VB version:

Listing 1:

Public Class ResponseHelper

Private Sub New()

End Sub

Public Shared Sub Redirect(ByVal url As String, ByVal target As String, ByVal windowFeatures As String)

Dim context As HttpContext = HttpContext.Current

If (String.IsNullOrEmpty(target) OrElse target.Equals("_self", StringComparison.OrdinalIgnoreCase)) AndAlso String.IsNullOrEmpty(windowFeatures) Then

context.Response.Redirect(url)

Else

Dim page As Page = CType(context.Handler, Page)

If page Is Nothing Then

Throw New InvalidOperationException("Cannot redirect to new window outside Page context.")

End If

url = page.ResolveClientUrl(url)

Dim script As String

If (Not String.IsNullOrEmpty(windowFeatures)) Then

script = "window.open(""{0}"", ""{1}"", ""{2}"");"

Else

script = "window.open(""{0}"", ""{1}"");"

End If

script = String.Format(script, url, target, windowFeatures)

ScriptManager.RegisterStartupScript(page, GetType(Page), "Redirect", script, True)

End If

End Sub

End Class

Listing 2:

Public Class ResponseHelper

Private Sub New()

End Sub

<System.Runtime.CompilerServices.Extension> _

Public Shared Sub Redirect(ByVal response As HttpResponse, ByVal url As String, ByVal target As String, ByVal windowFeatures As String)

If (String.IsNullOrEmpty(target) OrElse target.Equals("_self", StringComparison.OrdinalIgnoreCase)) AndAlso String.IsNullOrEmpty(windowFeatures) Then

response.Redirect(url)

Else

Dim page As Page = CType(HttpContext.Current.Handler, Page)

If page Is Nothing Then

Throw New InvalidOperationException("Cannot redirect to new window outside Page context.")

End If

url = page.ResolveClientUrl(url)

Dim script As String

If (Not String.IsNullOrEmpty(windowFeatures)) Then

script = "window.open(""{0}"", ""{1}"", ""{2}"");"

Else

script = "window.open(""{0}"", ""{1}"");"

End If

script = String.Format(script, url, target, windowFeatures)

ScriptManager.RegisterStartupScript(page, GetType(Page), "Redirect", script, True)

End If

End Sub

End Class

# re: Response.Redirect into a new window (with Extension Methods)

Wednesday, September 26, 2007 1:13 AM by InfinitiesLoop

Andy -- not a security check. It's main purpose is to resolve the url to one usable on the client. For example, "~/foo" may resolve to "/app/foo" if app is the root of the application.

# re: Response.Redirect into a new window (with Extension Methods)

Wednesday, September 26, 2007 2:15 AM by Nikhil Kothari

Actually you shouldn't really be calling Page.ResolveClientUrl - instead you should take in a reference/context control and use that to call ResolveClientUrl. I know... it complicates the signature...

The reason being - if I am calling this method from within a user control or a master page, then the resolution needs to happen with that context, rather than the page using them.

Personally I think this method belongs on ClientScriptManager, and not on HttpResponse.

# re: Response.Redirect into a new window (with Extension Methods)

Wednesday, September 26, 2007 7:27 AM by camby.xie

so cool ,it is so easy to use according to your idea!

# re: Response.Redirect into a new window (with Extension Methods)

Wednesday, September 26, 2007 10:24 AM by Siraj Gadhia

It is not big deal; I have been using same way since last 4 years!

# re: Response.Redirect into a new window (with Extension Methods)

Wednesday, September 26, 2007 10:28 AM by Siraj Gadhia

It is not big deal...!

I have been using same way since long time!

Simple way one can do as under …

       Dim _URL As String

       Dim JavaScript As String

       _URL = "Main.aspx"

       JavaScript = "window.open('" + _URL + "', null, 'resizable=yes,height=600,width=900, left=0, top=0, status=yes,toolbar=no,menubar=no,location=no')"

       Response.Write("<SCRIPT LANGUAGE=javascript>" & vbCrLf)

       Response.Write("<!--" & vbCrLf)

       Response.Write(JavaScript & vbCrLf)

       Response.Write("//-->" & vbCrLf)

       Response.Write("</SCRIPT>" & vbCrLf)

# re: Response.Redirect into a new window (with Extension Methods)

Wednesday, September 26, 2007 11:59 AM by Vikram

Good One.

# re: Response.Redirect into a new window (with Extension Methods)

Wednesday, September 26, 2007 1:40 PM by InfinitiesLoop

Nikhil -- I thought of that, but Response.Redirect doesn't resolve based on the current template control (unless it does something crazy I'm not aware of) so why should this?

I do think putting it on page would make more sense, which is almost the same as putting it on ClientScriptManager.

# re: Response.Redirect into a new window (with Extension Methods)

Wednesday, September 26, 2007 7:45 PM by Nikhil Kothari

Response.Redirect maps almost 1:1 with a 302 status code and is a pipeline event, which is independent of page vs. user control events. This API seems more about the page framework, and hence should account for resolving against the correct context... just an opinion.

# re: Response.Redirect into a new window (with Extension Methods)

Thursday, September 27, 2007 5:47 AM by Kumaran

hurray ! its really great.

# Response.Redirect 打开新窗口的两种方法

Friday, September 28, 2007 5:21 AM by 罗志威

一般情况下,Response.Redirect 方法是在服务器端进行转向,因此,除非使用 Response.Write(

# 10 Hot ASP.NET Tips and Tricks - 10/28/2007

Friday, September 28, 2007 6:02 AM by 10 Hot ASP.NET Tips and Tricks - 10/28/2007

Pingback from  10 Hot ASP.NET Tips and Tricks - 10/28/2007

# Response.Redirect 打开新窗口的两种方法——转

Friday, September 28, 2007 11:10 PM by 公子扬

# re: Response.Redirect into a new window (with Extension Methods)

Monday, October 01, 2007 8:40 AM by Ravi

Really good. COOL

# re: Response.Redirect into a new window (with Extension Methods)

Monday, October 01, 2007 1:23 PM by Josh Stodola

ROFL

I cant help but laugh my ass off at Siraj Gadhia's comment

# re: Response.Redirect into a new window (with Extension Methods)

Monday, October 01, 2007 1:30 PM by InfinitiesLoop

/nod

# re: Response.Redirect into a new window (with Extension Methods)

Monday, October 01, 2007 6:15 PM by Raul Macias

Good stuff

# re: Response.Redirect into a new window (with Extension Methods)

Tuesday, October 09, 2007 10:34 AM by Bob Vandehey

Nothing wrong with judicious use of GOTO's although that argument was made back when we were developing in VB6 and you didn't have structured exception handling. I have never used a GOTO in C# although I wouldn't have any problem using one if the situation called for it.

Thanks for the reference!

# re: Response.Redirect into a new window (with Extension Methods)

Tuesday, October 09, 2007 1:22 PM by InfinitiesLoop

Bob! You do read my blog. I'm honored. Now put it on the required company reading list :)

# re: Response.Redirect into a new window (with Extension Methods)

Wednesday, October 17, 2007 9:01 AM by JS

Hi,

This works ok on my local development machine but not on my shared hosted environment.

This line fails in the helper class:

ScriptManager.RegisterStartupScript(page, GetType(Page), "Redirect", script, True)

With the following error:

Compilation Error: BC30451: Name 'ScriptManager' is not declared.

Is this because I need AJAX for this to work.  My hoster doesn't allow AJAX?

# re: Response.Redirect into a new window (with Extension Methods)

Wednesday, October 17, 2007 1:45 PM by InfinitiesLoop

JS -- if you want it to work whether AJAX is available or not, this article is for you:

weblogs.asp.net/.../HOWTO_3A00_-Write-controls-compatible-with-UpdatePanel-without-linking-to-the-ASP.NET-AJAX-DLL.aspx

# Response.Redirect 打开新窗口的两种方法

Saturday, October 27, 2007 6:12 PM by gooddasenlin

Response.Redirect 打开新窗口的两种方法

# [导入]Response.Redirect 打开新窗口的两种方法

Wednesday, November 14, 2007 3:22 AM by 草秀于木

一般情况下,Response.Redirect方法是在服务器端进行转向,因此,除非使用Response.Write(

# [转贴]Response.Redirect 打开新窗口的两种方法

Thursday, November 15, 2007 7:26 AM by 雨過天晴

一般情况下,Response.Redirect 方法是在服务器端进行转向,因此,除非使用 Response.Write(

# Response.Redirect 打开新窗口的两种方法[转]

Monday, November 19, 2007 6:59 AM by 雨過天晴

一般情况下,Response.Redirect 方法是在服务器端进行转向,因此,除非使用 Response.Write(

# Response.Redirect 打开新窗口的两种方法

Thursday, March 20, 2008 8:46 AM by nply2008

Response.Redirect 打开新窗口的两种方法

# Response.Redirect 打开新窗口的两种方法

Wednesday, April 16, 2008 10:06 AM by jcc3120

adf

# Work in progress, Response.RelativeRedirect

Wednesday, April 23, 2008 6:00 PM by mostlylucid

Work in progress, Response.RelativeRedirect

# re: Response.Redirect into a new window (with Extension Methods)

Sunday, May 18, 2008 7:06 AM by PaulSinnema

Hi,

I'm a bit confused. Do I need Visual Studio 2008 to be able to use the 'this' keyword?

Paul.

# re: Response.Redirect into a new window (with Extension Methods)

Sunday, May 18, 2008 1:18 PM by InfinitiesLoop

PaulSinnema -- it's a C# 3.0 compiler feature. The short answer is yes you need 2008. But technically I dont think anything could stop you from using VS2005 to use C# 3.0, and it would build successfully even if VS didn't like the syntax, but I don't have any specific instructions on how to do that.

# re: Response.Redirect into a new window (with Extension Methods)

Tuesday, June 03, 2008 5:20 AM by rlp

Thanks for this nice code!

# Response.Redirect 打开新窗口的两种方法

Tuesday, June 17, 2008 1:43 AM by 蒋君

一般情况下,Response.Redirect方法是在服务器端进行转向,因此,除非使用Response.Write(

# re: Response.Redirect into a new window (with Extension Methods)

Sunday, July 06, 2008 6:08 AM by appl

hi this code works but may i know how to pass the value from the popup window to it parent widow?

# re: Response.Redirect into a new window (with Extension Methods)

Friday, September 19, 2008 7:57 AM by Pete Dashwood

This is an imaginative solution elegantly coded. The argument regarding override of Response is two edged. Perhaps it should be on Page, but we override methods every day if it gets the job done. Injecting script through this method won't please everyone. Personally, I like it.  I think the real problem is the schism between client and server (AJAX is kind of an attempt to address this) that prevents the Response Class from implementing this function itself, (maybe as an extension of or parameter to, Response.Redirect).  Thanks for posting it.

# re: Response.Redirect into a new window (with Extension Methods)

Monday, October 06, 2008 12:13 AM by bpd

thanks very useful!

I used current time as the name of the frame in order to load multiple unique tabs.

ResponseHelper.Redirect(vquery, DateTime.Now.Second.ToString(), "");

# re: Response.Redirect into a new window (with Extension Methods)

Monday, October 06, 2008 2:10 PM by InfinitiesLoop

bpd -- if you use _blank it will always open a new one, no need to try and make every name unique.

# re: Response.Redirect into a new window (with Extension Methods)

Tuesday, October 14, 2008 12:50 PM by Ron Woods

Absolutely ingenious!  Thank you!

# re: Response.Redirect into a new window (with Extension Methods)

Wednesday, October 22, 2008 10:08 AM by cavo789

Thank you for this really usefull code !

# re: Response.Redirect into a new window (with Extension Methods)

Saturday, October 25, 2008 5:56 AM by Nikhilesh

It's a nice work.

I've a problem. The code is running correctly in my local, but it is not running in my server (the page is hosted in manashosting.com).No error message is didplaying,

In button event i've given 2 function.

1. is to show a image

2. to redirect to another page.

the image is showing correctly but the new page is not opening.

And u've given

Response.Redirect("popup.aspx", "_blank", "menubar=0,width=100,height=100");

but for me it is working as like this

ResponseHelper.Redirect "Authenticated.aspx", "_blank", "width=550px; heigth=800px;");

pls give a correct and fine reply.

# re: Response.Redirect into a new window (with Extension Methods)

Friday, November 07, 2008 7:51 PM by Mayank

God bless you!

# re: Response.Redirect into a new window (with Extension Methods)

Saturday, December 13, 2008 6:37 AM by Seema

'ScriptManager' does not exist in the current context

Please tell me what is the issue with this.

Thanx

# re: Response.Redirect into a new window (with Extension Methods)

Tuesday, December 16, 2008 4:56 PM by Richard Elliott

This is EXACTLY what I needed.  Thanks so much!!!!!!!!

# re: Response.Redirect into a new window (with Extension Methods)

Saturday, December 20, 2008 3:15 AM by Scott Austin

Brilliant! Works like a charm. Cheers

# Response.Redirect 打开新窗口的两种方法

Wednesday, December 24, 2008 8:09 AM by 追梦人RUBY

一般情况下,Response.Redirect方法是在服务器端进行转向,因此,除非使用Response.Write(

# Response.Redirect 打开新窗口的两种方法

Saturday, January 10, 2009 2:51 AM by sandou

一般情况下,Response.Redirect方法是在服务器端进行转向,因此,除非使用Response.Write(

# re: Response.Redirect into a new window (with Extension Methods)

Tuesday, January 13, 2009 5:10 AM by Maxi

This is super useful!!!

thank you so much!

# re: Response.Redirect into a new window (with Extension Methods)

Monday, January 19, 2009 11:11 PM by $200

OMG I never thought it is really possible to open new window on server side like that. You are a genius :O.

Thanks heaps

# re: Response.Redirect into a new window (with Extension Methods)

Friday, January 30, 2009 12:22 PM by vbjox

Is there a way to redirect to a new window, and then have the parent window redirect to another window?

Example:  I have a page on which the user clicks a button that will open a new window.  Upon that happening, I want the page on which the user clicked the button to goto another page.

Currently, if I use the ResponseHelper, followed by a Response.Redirect, the Response.Redirect is never invoked.

Thanks.

# re: Response.Redirect into a new window (with Extension Methods)

Friday, January 30, 2009 2:37 PM by InfinitiesLoop

vbjox -- you will have to emit some script which sets window.location to the new page you want the main window to go to.

# Response.Redirect 打开新窗口的两种方法收藏

Wednesday, February 04, 2009 9:48 PM by 董士亮

一般情况下,Response.Redirect方法是在服务器端进行转向,因此,除非使用Response.Write(

# re: Response.Redirect into a new window (with Extension Methods)

Friday, February 27, 2009 7:17 PM by RabidDog

Thanks so much. Saved me a lot of heartache  :)

# re: Response.Redirect into a new window (with Extension Methods)

Wednesday, March 18, 2009 9:32 AM by salma11

Thank you very very very much

# re: Response.Redirect into a new window (with Extension Methods)

Wednesday, April 08, 2009 10:59 AM by Helen Zhou

Thank you for the codes. I put the codes in an imagebutton click event. The codes enable me to click an imagebutton in a gridview to open a new page with enlarged image displayed. But the problem is the new page hides behind its parents' page. This is probably because after the new page loads, the parents' page runs its page load again. This leads the new page losts focus. Can someone help me with the issue? Thanks.

# re: Response.Redirect into a new window (with Extension Methods)

Saturday, April 25, 2009 3:05 PM by vi

thanks for billions times.

# re: Response.Redirect into a new window (with Extension Methods)

Sunday, June 07, 2009 4:44 PM by Shane

This was a very elegant solution. I carried over varibles to the pop up via session varibles. I am not sure that is the best way but it seems to work easy enough.

# re: Response.Redirect into a new window (with Extension Methods)

Tuesday, June 23, 2009 10:59 PM by tony

great, finally i can call pop up windows from a pop up. thanks

# re: Response.Redirect into a new window (with Extension Methods)

Thursday, July 09, 2009 7:13 PM by Sertac

First i want to congratulate you for your brilliant solution. Is there any way to get past poup blockers?

Thanks again.

# re: Response.Redirect into a new window (with Extension Methods)

Thursday, July 16, 2009 10:36 AM by mike

I like it and I'm using for my project.  But Micro$oft doesn't seem to like extension methods very much.  From the M$ documentation:

In general, we recommend that you implement extension methods sparingly and only when you have to. Whenever possible, client code that must extend an existing type should do so by creating a new type derived from the existing type. For more information, see Inheritance (C# Programming Guide).

When using an extension method to extend a type whose source code you cannot change, you run the risk that a change in the implementation of the type will cause your extension method to break.

Bunch of whiners.

# re: Response.Redirect into a new window (with Extension Methods)

Thursday, July 16, 2009 12:50 PM by InfinitiesLoop

Mike, you're kidding right? :) They add a feature and then give guidance on how best to use it and you think it's whining? Wow.

# re: Response.Redirect into a new window (with Extension Methods)

Thursday, July 30, 2009 6:43 AM by Luis Miguel Molinero

Your explanation and the code are very good, but please change the background color, is absolutely unreadable. Why not use the humble and traditional white background or another soft color?

Thanks

# re: Response.Redirect into a new window (with Extension Methods)

Monday, August 03, 2009 8:46 AM by George Sifakis

Let me also add my thanks for this excellent piece of code. It's probably the only way to pop a page from the "Select" button of a GridView. I used it and it works fine. Now, if I only could get around the problem of the new page hiding behind the old one ...

# re: Response.Redirect into a new window (with Extension Methods)

Monday, August 03, 2009 2:11 PM by Danny T.

This is a great idea and it almost got me where I wanted to go...

I use your piece of code to open multiple windows, but I want to use query string parameters in my urls.

However, as soon as I add "?Parameter=value", the new windows all display "404 not found" with the correct url in the address bar. I press enter in the new windows and they display the page I wanted.

Do you know why?

# re: Response.Redirect into a new window (with Extension Methods)

Tuesday, August 04, 2009 5:41 AM by George Sifakis

Solution to the problem of the newly created page hiding behind its parent:

Add the following  in the child page:

<body  onload="javascript:self.focus()">

# re: Response.Redirect into a new window (with Extension Methods)

Wednesday, August 12, 2009 11:46 AM by Sergio

Thanks for the solution.

Regards.

# re: Response.Redirect into a new window (with Extension Methods)

Monday, August 17, 2009 3:28 PM by Don

This is great!  I too used it to pop a page from a grid, and it works great.  And it is the appropriate use of an Extension method in that the HttpResponse class is sealed.

# re: Response.Redirect into a new window (with Extension Methods)

Wednesday, August 19, 2009 9:41 AM by dharam_hbtik

I am not able to implement it in my page.

please help me how can i  use this code?

should i write the above given code in new .cs file or in the same aspx.cs file?

how can i overload  the response.redirect method?

# re: Response.Redirect into a new window (with Extension Methods)

Wednesday, August 26, 2009 10:34 AM by jj_berg

If it works, it works.  I don't care about standards and all that hoopala.  Thanks for making it simple.  :-)

# re: Response.Redirect into a new window (with Extension Methods)

Friday, September 11, 2009 12:14 PM by Siva

Dear Author

Thank you for sharing this great article.

Into my doubts

Whats Script Manager in the code?

I am Processing the request inside a class def. I cant manage the script manager there ( if u intends ajax script manager)

How can i do this

# re: Response.Redirect into a new window (with Extension Methods)

Thursday, September 17, 2009 6:51 AM by jmx

I've got a problem with this code using IE (currently version 8 maybe it occurs in previous version also), the newly created window/tab still don't get any focus. It works fine on other browsers.

After trying "onload="javascript:self.focus()" on new window it still don't work. Is there any solution for this ? Thanks in advance.

# re: Response.Redirect into a new window (with Extension Methods)

Thursday, October 08, 2009 7:51 PM by Richard

Thanks.  I put it good use immediately!

# re: Response.Redirect into a new window (with Extension Methods)

Friday, October 23, 2009 6:23 AM by Polprav

Hello from Russia!

Can I quote a post in your blog with the link to you?

# re: Response.Redirect into a new window (with Extension Methods)

Friday, October 23, 2009 1:40 PM by InfinitiesLoop

Polprav -- go for it :)

# re: Response.Redirect into a new window (with Extension Methods)

Thursday, October 29, 2009 7:19 AM by gvdamn

Can I use your (extension) method also to open up e.g. an Acrobat reader file? If so any help in the (preferably VB) code to make this happen would be much appreciated? (The file is coming from a database and contained in a Byte variable.)

# re: Response.Redirect into a new window (with Extension Methods)

Wednesday, November 18, 2009 10:15 AM by Hasu

Hi Everyone

when i implement the above functionality i get the following error

"object reference not set to an instance of an object"

can any one help me to implement it step by step

# re: Response.Redirect into a new window (with Extension Methods)

Thursday, November 19, 2009 11:10 AM by Orlando15767

excellent solution for a very common and frustraiting issue many developers come across.

# re: Response.Redirect into a new window (with Extension Methods)

Friday, November 20, 2009 6:30 AM by davetiyeci

düğün davetiyesi ve davetiye sözleri

Leave a Comment

(required) 
(required) 
(optional)
(required)