从ASP.net代码隐藏有条件地关闭jquery模式窗口

时间:2012-09-18 20:13:31

标签: javascript jquery asp.net colorbox

我有一个ASP.net页面,其中包含一个链接,用于打开基于jquery的模态窗口(使用colobox jquery插件)。从另一个aspx文件加载的那个窗口的内容(它加载一个iframe)。当用户按下asp:按钮并且我的代码隐藏中的某些条件进展顺利时,我想关闭该窗口。

我尝试了许多方法从代码隐藏中关闭该窗口,如下所示:

Page.RegisterStartupScript("X", @"$(this).dialog('close');");

Page.RegisterStartupScript("X", @"var win = window.open('','_self'); win.close();");

btnDone.Attributes.Add("onclick", @"var win = window.open('','_self'); win.close();");

btnDone.Attributes.Add("onclick", @"window.open('../px/nsk.aspx', '_self', null); window.close(); ");

System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>");
            System.Web.HttpContext.Current.Response.Write("self.close();");
            System.Web.HttpContext.Current.Response.Write("</SCRIPT>");

但是没有它们可以关闭该模态窗口。我正在测试最新版本的firefox。 后面的代码可以假设为这样:

    // do some database works
if (condition)
{
   // close this modal window
}

我也尝试过jquery的方法,但没有一个是成功的。 你能告诉我怎么能关闭这个窗口吗?

2 个答案:

答案 0 :(得分:0)

问题接缝以这种方式解决:

    Page.ClientScript.RegisterStartupScript(GetType(), 
        "CloseKey", "parent.$.colorbox.close();", true);

答案 1 :(得分:0)

使用带有div而不是iframe的jQuery对话框。

然后,您可以使用Ajax帖子运行服务器端代码并​​查看结果以决定如何处理对话框。

声明对话框:

    $(document).ready(function () {
        jQuery("#MyDialog").dialog(
            {
                bgiframe: true,
                autoOpen: false,
                modal: true,
                width: 800,
                position: ['center', 100]
            }
        );
    });

填充对话框并将其打开:

    $('#OpenMyDialog').click(function () {
        $.post($('url_for_dialog_contents'), function (data) {
            $("#MyDialog").empty();
            $("#MyDialog").append(data);
        }, null, "html");
        $('#MyDialog').dialog('open');
        return false;
    });

提交对话框:

    $('#SubmitMyDialog').click(function () {
        $.post($('url_for_dialog_action'), function (data) {
            if (data.success == true) { $('#MyDialog').dialog('close'); }
        }, null, "json");
    });

我确信在提交对话框中你需要更多的代码,但这是一般的想法。

相关问题