ClientScript.RegisterStartupScript不起作用

Csh*_*arp 6 javascript asp.net clientscript registerstartupscript

我搜索了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);
        }
    }
}  
Run Code Online (Sandbox Code Playgroud)

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

//<![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'));
}
Run Code Online (Sandbox Code Playgroud)

小智 7

你可以改用它

ScriptManager.RegisterStartupScript(this.Page, GetType(), "ClosePopup", "window.close();", true);
Run Code Online (Sandbox Code Playgroud)

或者你也可以尝试这个

Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "ClosePopup", "window.close();", true);
Run Code Online (Sandbox Code Playgroud)

祝你有美好的一天.


Csh*_*arp 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>
Run Code Online (Sandbox Code Playgroud)