janhartmann
janhartmann

Reputation: 15003

Open new window from code-behind

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.

Upvotes: 4

Views: 15007

Answers (5)

Brad Parks
Brad Parks

Reputation: 71961

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!

Upvotes: 4

Bobby Borszich
Bobby Borszich

Reputation: 11767

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.

Upvotes: 0

iburlakov
iburlakov

Reputation: 4232

Simple sample of RegisterStartupScript:

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

Upvotes: 0

Robin Day
Robin Day

Reputation: 102458

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.

Upvotes: 4

Kostas Konstantinidis
Kostas Konstantinidis

Reputation: 13707

You will need to run javascript on postback

Upvotes: 1

Related Questions