【问题标题】:Javascript function is not being called from code behindJavascript函数没有从后面的代码中调用
【发布时间】:2014-03-05 18:01:45
【问题描述】:

我有一个像

这样的 javascript 函数
function CloseWindow() {
    alert("I am firing");    // window.close();         
}

我正在尝试从按钮单击事件后面的代码中调用它,例如

Page.ClientScript.RegisterStartupScript([GetType](), "Javascript", "javascript:CloseWindow();", True)

该函数未触发警报消息。 如果我从 OnClientClick 调用相同的函数,它就会触发。

OnClientClick="javascript:();"

可能是什么原因?如果您想进一步澄清,请告诉我。

<script type="text/javascript" language="javascript"> function CloseWindow() { alert("I am firing"); // window.close();
} function chkLength(evt, len) { var str = document.getElementById(evt.id); if (str.value.length < len) return true; else return false; } </script>

【问题讨论】:

  • 能否提供围绕javascript代码的代码?
  • 我用 VB.Net 标记,每个人都在 c# 中回答:(
  • @Johan 检查我的脚本部分。

标签: javascript asp.net vb.net code-behind


【解决方案1】:

如果您使用了更新面板,那么您可以使用:

ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "javascriptFunction();", true);

您可以使用的其他方法

ClientScript.RegisterStartupScript
        (GetType(),Guid.NewGuid().ToString(), "javascriptFunction();",true);

【讨论】:

    【解决方案2】:

    试试下面的代码....

    ClientScript.RegisterStartupScript
            (GetType(),Guid.NewGuid().ToString(), "javascript: CloseWindow();",true);
    

    【讨论】:

    • VB.Net中是true还是True
    【解决方案3】:

    第三个参数(脚本)必须是“CloseWindow();”而不是“javascript:CloseWindow();”

    【讨论】:

    • 确保在您的 html 页面的最顶部声明​​了函数“CloseWindow”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多