简体   繁体   中英

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.

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!

你需要在回发时运行javascript

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM