A very dirty trick to open a Popup after a PostBack operation
I’m not very proud of this cause it indicates a very poor interface design, but in a legacy project I’m working on we had the following scenario:
1- the user interact on some elements of the UI
2- the user clicks a button, at the PostBack operation information are gathered about the UI status and what the user done on some elements
3- at the end of the processing we needed to open a popup showing the result of the operation.
The problem is: opening a popup is a client-side operation, while all the processing is done server-side.
The trick is simple: at the end of the PostBack operation just inject some Javascript code that when executed at the client side (after all the DOM of the page has been constructed) open the popup with the usual window.open() function call.
You do that using the Page.ClientScript.RegisterStartupScript() call, but this one will not work if you try to use it inside an Ajax UpdatePanel; to solve the problem you have to use the ‘new’ ScriptManager.RegisterStartupScript() call, here’s an example:
Public Function CreateWindowsOpenFunction(ByVal link As String) As String
link = Navigation.AdjustLinkForVirtualPath(link)
Return String.Format("javascript: window.open('{0}', '_blank');", link)
End Function
Private Sub OpenInAnotherWindow(byval url as string)
Dim script As String = CreateWindowsOpenFunction(url)
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "navigation", script, True)
End Sub
You can call the OpenInAnotherWindow() function at the end of your processing, when the page will be reloaded by the browser (after the PostBack) the window.open() script will be executed.
By the way, in the example above I used GetType() as the second argument of the call, but you really should avoid doing it, take a look at this post: Don't use GetType() with Page.ClientScript.RegisterClientScriptBlock()
If you have to start using tricks like these, maybe it’s better to start rethinking the UI and the data flow of your application.

#1 da Thierry - Saturday March 2010 alle 01:15
Just to add my 2 cents, it might work out of the box on old IE installations, but any recent browser (safari, chrome, firefox, IE7 and +) will block javascript window.open() calls that don't result from a direct link click of html button push. They just show "a popup has been blocked" variant to the user. So, the user still must manually allow the popups from your site. As you said, this is a drity trick, and pretty disrupive to the ui flow.
#2 da mjPYT - Saturday March 2010 alle 01:15
Interesting post - i stumbled across your article by surprise, thank you for the article!