C# 如何在按钮单击事件上打开新的浏览器窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9056103/
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
How to open new browser window on button click event?
提问by Rushikesh
How to open new browser window on button click event in C# ASP.NET?
如何在 C# ASP.NET 中的按钮单击事件上打开新的浏览器窗口?
Please share any example.
请分享任何例子。
I am doing following code. Please let me know where I am going wrong.
我正在做以下代码。请让我知道我哪里出错了。
btn_Click()
{
if(condition==true)
{
this.Page.ClientScript.RegisterStartupScript(
this.GetType(),
"page_index_script2",
"openNewWindow();",
true
);
}
}
And the JavaScript function is
JavaScript 函数是
function openNewWindow()
{
alert('HI');
window.open('http://www.stackoverflow.com');
}
When I run the code from javascript function Alert works but new window is not getting opened.
当我从 javascript 函数 Alert 运行代码时,但没有打开新窗口。
采纳答案by DotNetUser
You can use some code like this, you can adjust a height and width as per your need
您可以使用一些这样的代码,您可以根据需要调整高度和宽度
protected void button_Click(object sender, EventArgs e)
{
// open a pop up window at the center of the page.
ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", "var Mleft = (screen.width/2)-(760/2);var Mtop = (screen.height/2)-(700/2);window.open( 'your_page.aspx', null, 'height=700,width=760,status=yes,toolbar=no,scrollbars=yes,menubar=no,location=no,top=\'+Mtop+\', left=\'+Mleft+\'' );", true);
}
回答by Kristian
Response.Write('... javascript that opens a window...')
Response.Write('...打开窗口的javascript...')
回答by FishBasketGordo
回答by Thomas
Or write to the response stream:
或写入响应流:
Response.Write("<script>");
Response.Write("window.open('page.html','_blank')");
Response.Write("</script>");

