ClientScript.RegisterStartupScript not working

ClientScript.RegisterStartupScript not working

本文关键字:working not RegisterStartupScript ClientScript      更新时间:2023-09-26

我已经搜索了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);
        }
    }
}  

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

//<![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'));
}

你可以用这个代替

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

或者你也可以试试这个

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

祝你有美好的一天

因为我不能让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>