ClientScript.RegisterStartupScript不起作用

时间:2013-05-10 14:47:17

标签: javascript asp.net registerstartupscript clientscript

我搜索了SO&谷歌,但我似乎无法让这个工作。 代码在我的asp.net应用程序中的“取消”按钮的代码隐藏点击事件中,但似乎没有关闭弹出窗口。有什么想法吗?

try
{
    if (btnCancel.Text == "Close")
    {
        String csName1 = "PopupScript";
        Type csType = this.GetType();

        ClientScriptManager cs = Page.ClientScript;
        if (!cs.IsClientScriptBlockRegistered(csType, csName1))
        {
            ClientScript.RegisterStartupScript(GetType(), "ClosePopup", "window.close();", true);
        }
    }
}  

更新:回发后,当我查看源页面时,我看到的唯一相关代码是:

//<![CDATA[
(function() {var fn = function() {$get("ToolkitScriptManager1_HiddenField").value = '';Sys.Application.remove_init(fn);};Sys.Application.add_init(fn);})();window.close();
document.getElementById('ValidationSummary1').dispose = function() {
    Array.remove(Page_ValidationSummaries, document.getElementById('ValidationSummary1'));
}

2 个答案:

答案 0 :(得分:7)

您可以改用

ScriptManager.RegisterStartupScript(this.Page, GetType(), "ClosePopup", "window.close();", true);

或者您也可以试试这个

Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "ClosePopup", "window.close();", true);

祝你有个美好的一天。

答案 1 :(得分:1)

因为我无法让ClientScript按需运行,所以我使用下面的代码做了一个解决方法:

    function closeWin() {
        //If txt = 'cancel' then close;
        GetRadWindow().Close();
    }


<td align="center"><asp:Button runat="server" ID="btnClose" Text="Close" 
        OnClientClick="closeWin();return false;" onclick="btnClose_Click"/></td>