C# 从后面的代码以编程方式关闭aspx页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/375406/
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
Programmatically close aspx page from code behind
提问by Michael Kniskern
What is the best way to close an ASPX page from the code-behind?
从代码隐藏关闭 ASPX 页面的最佳方法是什么?
I have a button event handler that I want to close the page after the user has clicked an ASP.NET button on the page. I have tried to programmatically add a JavaScript method that contains a window.close()
command to the OnClientClick
event to close the page but it does not work. The button is also a asp:AsyncPostBoskTrigger
for an AJAX Update Panel.
我有一个按钮事件处理程序,我想在用户单击页面上的 ASP.NET 按钮后关闭页面。我试图以编程方式添加一个 JavaScript 方法,该方法包含关闭页面window.close()
的OnClientClick
事件命令,但它不起作用。该按钮也是asp:AsyncPostBoskTrigger
AJAX 更新面板的一个。
The application uses .NET Framework 3.5.
该应用程序使用 .NET Framework 3.5。
采纳答案by Michael Kniskern
UPDATE:I have taken all of your input and came up with the following solution:
更新:我已经听取了您的所有意见并提出了以下解决方案:
In code behind:
在后面的代码中:
protected void Page_Load(object sender, EventArgs e)
{
Page.ClientScript.RegisterOnSubmitStatement(typeof(Page), "closePage", "window.onunload = CloseWindow();");
}
In aspx page:
在aspx页面中:
function CloseWindow() {
window.close();
}
回答by Joel Coehoorn
A postback is the process of re-loading a page, so if you want the page to close after the postback then you need to set your window.close() javascript to run with the browser's onload event during that postback, normally done using the ClientScript.RegisterStartupScript()
function.
回发是重新加载页面的过程,因此如果您希望页面在回发后关闭,那么您需要将 window.close() javascript 设置为在回发期间与浏览器的 onload 事件一起运行,通常使用ClientScript.RegisterStartupScript()
功能。
But are you sure this is what you want to do? Closing pages tends to piss off users.
但你确定这是你想要做的吗?关闭页面往往会惹恼用户。
回答by EndangeredMassa
You should inject a startup script that will close the page after the postback has finished.
您应该注入一个启动脚本,该脚本将在回发完成后关闭页面。
ClientScript.RegisterStartupScript(typeof(Page), "closePage", "<script type='text/JavaScript'>window.close();</script>");
回答by CodingWithSpike
You would typically do something like:
您通常会执行以下操作:
protected void btnClose_Click(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(typeof(Page), "closePage", "window.close();", true);
}
However, keep in mind that different things will happen in different scenerios.
Firefox won't let you close a window that wasn't opened by you (opened with window.open()
).
但是,请记住,在不同的场景中会发生不同的事情。Firefox 不会让您关闭不是由您打开的窗口(用 打开window.open()
)。
IE7 will prompt the user with a "This page is trying to close (Yes | No)" dialog.
IE7 将提示用户“此页面正在尝试关闭(是 | 否)”对话框。
In any case, you should be prepared to deal with the window not always closing!
无论如何,您应该准备好处理并非总是关闭的窗口!
One fix for the 2 above issues is to use:
解决上述 2 个问题的一种方法是使用:
protected void btnClose_Click(object sender, EventArgs e) {
ClientScript.RegisterStartupScript(typeof(Page), "closePage", "window.open('close.html', '_self', null);", true);
}
And create a close.html:
并创建一个 close.html:
<html><head>
<title></title>
<script language="javascript" type="text/javascript">
var redirectTimerId = 0;
function closeWindow()
{
window.opener = top;
redirectTimerId = window.setTimeout('redirect()', 2000);
window.close();
}
function stopRedirect()
{
window.clearTimeout(redirectTimerId);
}
function redirect()
{
window.location = 'default.aspx';
}
</script>
</head>
<body onload="closeWindow()" onunload="stopRedirect()" style="">
<center><h1>Please Wait...</h1></center>
</body></html>
Note that close.html will redirect to default.aspx if the window does not close after 2 sec for some reason.
请注意,如果窗口在 2 秒后由于某种原因没有关闭,则 close.html 将重定向到 default.aspx。
回答by Perpetualcoder
You can close a window by simply pasting the window closing code in the button's OnClientClick event in the markup
您可以通过简单地将窗口关闭代码粘贴到标记中按钮的 OnClientClick 事件中来关闭窗口
回答by SP007
protected void btnOK_Click(object sender, EventArgs e)
{
// Your code goes here.
if(isSuccess)
{
string close = @"<script type='text/javascript'>
window.returnValue = true;
window.close();
</script>";
base.Response.Write(close);
}
}
回答by Hasna Ashraf
For closing a asp.net windows application,
要关闭一个 asp.net windows 应用程序,
- Just drag a button into the design page
Then write the below given code inside its click event
" this.close(); "
- 只需将一个按钮拖入设计页面
然后在它的点击事件中编写下面给定的代码
" this.close(); "
ie:
IE:
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
回答by Lasco
You just need to add this property in your asp:Button element (for example):
你只需要在你的 asp:Button 元素中添加这个属性(例如):
OnClientClick="javascript:window.close();"
It works perfectly.
它完美地工作。
回答by Vijender Reddy Chintalapudi
If you using RadAjaxManager then here is the code which helps:
如果您使用 RadAjaxManager,那么这里的代码有帮助:
RadAjaxManager1.ResponseScripts.Add("window.opener.location.href = '../CaseManagement/LCCase.aspx?" + caseId + "';
window.close();");
回答by sakthivel
protected void Button1_Click(object sender, EventArgs e)
{
Button1.Attributes.Add("onclick"," CloseWindow();");
}
<script type="text/javascript">
function CloseWindow()
{
window.close();
}
</script>