如何在Asp.NET WebForms中保存后关闭模式窗口

时间:2013-09-10 09:40:19

标签: c# javascript asp.net webforms

我的主页上有一些javascript代码,当点击链接时会弹出一个弹出窗口:

<script language="javascript">  

function OpenResidentialAddressWin(subscriberContactRelationGid, routeId, btn) {
    window.showModalDialog("SubscriberResidentialAddress.aspx" + BuildQueryStringValuesForSubscriber(subscriberContactRelationGid, routeId, returntxtReceiptDate().value), this, strWindowFeatures + ";scroll:no;dialogWidth:442px;dialogHeight:350px");
    location.href = location.href;
}

</script>

因此,上面的代码在模态窗口中打开了SubscriberResidentialAddress.aspx页面(以及将许多变量传递到此页面中)

现在,此页面的排列方式是显示或编辑信息。 (这些是显示或隐藏的单独div,取决于窗口中刚按了哪些按钮)。

目前,当用户对页面上的信息进行保存后,它会切换到显示模式。相反,我想完全关闭模态窗口并重新加载主页面。

一位同事建议我在模态窗口的aspx页面上使用文字控件,并在执行成功保存后向其中写入javascript。所以我将以下内容放在SubscriberResidentialAddress.aspx页面的head部分:

<asp:Literal runat="server" ID="litCloseWind"></asp:Literal>

然后在执行保存时调用的函数后面的代码中

  protected void btnAddSave_Click(object sender, EventArgs e)
{

////// code related to saving

 if (status.IsSuccessful)
            {
                litCloseWind.Text = "<script>this.close()</script>";
                Response.Redirect(Request.Url.AbsoluteUri, false);
            }


}

我已经尝试了上面的内容并且litCloseWind.Text =“window.close()”;但是在执行保存后,两者都没有成功关闭模态窗口。

这个计划在逻辑上是否合理,我刚刚在某个地方犯了错误,或者这是一个不好的方式开始这么做?

1 个答案:

答案 0 :(得分:0)

为什么不在这里注册脚本,如下所示:

ClientScript.RegisterStartupScript(typeof(Page), "CLOSEWINDOW", "window.close();");

无需在此处添加任何文字控制。

所以你的代码看起来像这样:

protected void btnAddSave_Click(object sender, EventArgs e)
{
    if (status.IsSuccessful)
    {
        ClientScript.RegisterStartupScript(typeof(Page), "CLOSEWINDOW", "window.close();");
    }
}