Open new window from code-behind

14,490

Solution 1

You will need to run javascript on postback

Solution 2

You can use RegisterStartupScript to send a window.open script to run once the page has loaded.

However, this will cause the majority of popup blockers to get in your way.

Solution 3

I think this should work ;-)

Add some javascript to your radio button to open a new blank window before you post back. This makes it so popup blockers won't block your popup, since it's opened in response to a users click. See this link for how to do this part.

Then, allow the postback to happen as normal and on page load, register a startup script to tell your already existing window to go to a new url.

 String script = "window.open('popupPage.aspx', 'myPopup')";
 ScriptManager.RegisterStartupScript(Page, Page.GetType(), "someId", script, true);

Note that in javascript, when you call

 window.open(url, 'myPopup')

if a window already exists with that name it'll return it instead of creating a new window... So your popup won't get blocked!

Share:
14,490
janhartmann
Author by

janhartmann

Hello! I'm Jan Hartmann. With a wide set of skills, I enjoy building awesome, highly effective and performing web- and mobile and applications. http://dk.linkedin.com/in/janhartmanndk

Updated on June 05, 2022

Comments

  • janhartmann
    janhartmann almost 2 years

    I need to open a new window from code-behind on post-back if a specific radio button is selected.

    Is there any way to do this?

    Thank you.

    • epascarello
      epascarello almost 15 years
      FYI: Pop up blockers will block this window from opening.
  • NewXcoder
    NewXcoder over 10 years
    This appears to work very well. Thanks for posting this answer long after the other answer was accepted! You saved me a lot of grief.
  • NewXcoder
    NewXcoder over 10 years
    See @BradParks answer here for a good idea on how to fool popup blockers.