Question

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.

Was it helpful?

Solution

You will need to run javascript on postback

OTHER TIPS

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.

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!

Simple sample of RegisterStartupScript:

RegisterStartupScript("id1", "<script type=\"text/javascript\">alert(\"I'm from JavaScript.\");</script>");

New windows can be very sketchy, depending on the content that you need to present you might consider using an in window pop-in if you will. You will avoid pop-up blockers that way. If you can give more details we can give better answers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top