嗨,我有一个网络表单,我必须从代码隐藏文件中调用 JavaScript 函数。它工作正常,直到我向页面添加更新面板。 我使用的代码是
Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "OpenExe()", true);
我研究了一下,我发现如果我们在页面中添加脚本管理器/更新面板,我们必须使用
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "Sample", "OpenExe()", true);
我用过这个,我也用过
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(@"<script language='javascript'>");
sb.Append(@"var cmdShell = new ActiveXObject('WScript.Shell');");
sb.Append(@"var myPath = '\'C:\\Temp\\stacks.exe\\'');");
sb.Append(@"</script>");
if (!ClientScript.IsStartupScriptRegistered("JSScript"))
{
ClientScript.RegisterStartupScript(this.GetType(), "JSScript", sb.ToString());
}
我也尝试过
ScriptManager.RegisterStartupScript(UpdatePanel1, typeof(UpdatePanel), "Sample", "OpenExe()", true);
但是 JavaScript 没有执行。有人可以告诉我代码可能有什么问题吗?如果您想查看更多代码,请告诉我。提前致谢
最佳答案
好的,如果您有更新面板,请尝试以下操作:
ScriptManager.RegisterStartupScript(yourupdatepanel, this.GetType(), "Sample", "OpenExe()", true);
将 updatepanel 控件 ID 放在第一个参数上
关于c# - 无法在 Aspx 页面中从 Codebehind 运行 Javascript 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23904268/