0

我有一个“保存并关闭”按钮,我需要在保存数据后关闭我的页面。我尝试了很多方法,但都没有奏效。

代码隐藏:

protected void btnSaveClose_Click(object sender, EventArgs e)
{
    Save();
    ClientScript.RegisterClientScriptBlock(Page.GetType(), "script", "window.close();", true);
}

我需要帮助关闭窗口。

4

5 回答 5

2

该方法的MDN 文档解释了close一些限制:

此方法只能在使用该Window.open()方法的脚本打开的窗口上调用。如果该窗口不是由脚本打开的,则控制台中会出现与此类似的错误:Scripts may not close windows that were not opened by script.

换句话说,除非使用该方法打开窗口,否则该方法根本不起作用Window.open()

于 2013-11-01T21:27:50.437 回答
0

您不会在 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>

只需在代码隐藏中的适当位置切换这些面板的可见属性。

于 2013-11-01T21:36:41.987 回答
0

谢谢你们。这是修复它的解决方案。

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);
    }
}
于 2013-11-07T20:16:06.137 回答
0
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

这对我有用 谢谢大家

于 2014-03-22T18:02:55.683 回答
0

这对我有用,所以我会推荐它:

protected void Page_Load(object sender, EventArgs e)
{
    this.CloseBtn.Attributes.Add("OnClick", "self.close()");
}

protected void CloseBtn_Click(object sender, EventArgs e)
{
    Response.Write("<script language='javascript'> { self.close() }</script>");
}
于 2021-01-24T23:27:36.793 回答