关闭按钮上的页面单击 asp.net c#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19735654/
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
Close the Page on a Button Click asp.net c#
提问by Dextere
I have a save & close button where I need to close my page after saving the data. I tried it many ways but none worked.
我有一个保存并关闭按钮,我需要在保存数据后关闭我的页面。我尝试了很多方法,但没有一个奏效。
Code Behind:
背后的代码:
protected void btnSaveClose_Click(object sender, EventArgs e)
{
Save();
ClientScript.RegisterClientScriptBlock(Page.GetType(), "script", "window.close();", true);
}
I need help closing the window.
我需要帮助关闭窗口。
回答by p.s.w.g
The MDN documentationon the close
method explains some of the limitations:
关于该方法的MDN 文档close
解释了一些限制:
This method is only allowed to be called for windows that were opened by a script using the window.openmethod. If the window was not opened by a script, the following error appears in the JavaScript Console:
Scripts may not close windows that were not opened by script.
仅允许为使用window.open方法由脚本打开的窗口调用此方法。如果该窗口不是由脚本打开的,则 JavaScript 控制台中会出现以下错误:
Scripts may not close windows that were not opened by script.
In other words, this method simply won't work unless the window was opened with the window.open
method.
换句话说,除非使用该window.open
方法打开窗口,否则此方法根本无法工作。
回答by IrishChieftain
You don't "close windows" in a Web application. Simply display a friendly message to the user as confirmation or a friendly error message, whichever the case may be. You can easily do this using three panels:
您不会在 Web 应用程序中“关闭窗口”。简单地向用户显示一条友好的消息作为确认或一条友好的错误消息,无论哪种情况。您可以使用三个面板轻松完成此操作:
<asp:Panel id="formPanel" runat="server">
<!-- Your form controls go here -->
</asp:Panel>
<asp:Panel id="confirmPanel" runat="server">
<!-- Confirmation message -->
</asp:Panel>
<asp:Panel id="errorPanel" runat="server">
<!-- Error message -->
</asp:Panel>
Just toggle the visible property of these panels at the appropriate points in your code-behind.
只需在代码隐藏的适当位置切换这些面板的可见属性即可。
回答by Dextere
Thank you all. Here is the solution which fixed it.
谢谢你们。这是修复它的解决方案。
protected void Page_PreRender(object sender, EventArgs e)
{
if (ViewState["closewindow"] != null)
closewindow = (bool)ViewState["closewindow"];
if (closewindow)
{
closewindow = false;
ScriptManager.RegisterStartupScript(this, this.GetType(), "Close_Window", "self.close();", true);
///System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Close_Window", "self.close();", true);
}
}
回答by user3450375
string display = "Your dispaly";
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + display + "');", true);//this will dispaly the alert box
ScriptManager.RegisterStartupScript(this, this.GetType(), "Close_Window", "self.close();", true);//this will close the page on button click
This Worked for me Thanks to all
这对我有用 谢谢大家