C# 从代码隐藏打开新窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/897343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 22:03:51  来源:igfitidea点击:

Open new window from code-behind

c#asp.netjavascriptcode-behind

提问by janhartmann

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.

谢谢你。

采纳答案by Konstantinos

You will need to run javascript on postback

您将需要在回发时运行javascript

回答by Robin Day

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

您可以使用 RegisterStartupScript 发送 window.open 脚本以在页面加载后运行。

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

但是,这会导致大多数弹出窗口阻止程序妨碍您。

回答by iburlakov

Simple sample of RegisterStartupScript:

RegisterStartupScript 的简单示例:

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

回答by Bobby Borszich

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.

新窗口可能非常粗略,取决于您需要呈现的内容,如果愿意,您可以考虑使用窗口内弹出窗口。这样您将避免弹出窗口阻止程序。如果您能提供更多详细信息,我们可以提供更好的答案。

回答by Brad Parks

I thinkthis should work ;-)

认为这应该有效;-)

Add some javascript to your radio button to open a new blank window beforeyou 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 linkfor how to do this part.

您回发之前,向您的单选按钮添加一些 javascript 以打开一个新的空白窗口。这使得弹出窗口阻止程序不会阻止您的弹出窗口,因为它是在响应用户点击时打开的。有关如何执行此部分的信息,请参阅此链接

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.

然后,允许回发正常进行,并在页面加载时注册一个启动脚本,告诉您已经存在的窗口转到新的 url。

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

Note that in javascript, when you call

请注意,在 javascript 中,当您调用

 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!

如果一个窗口已经存在,它会返回它而不是创建一个新窗口......所以你的弹出窗口不会被阻止!