c# - 无法在 Aspx 页面中从 Codebehind 运行 Javascript 代码

标签 c# javascript asp.net ajax

嗨,我有一个网络表单,我必须从代​​码隐藏文件中调用 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/

相关文章:

c# - 如果按下特定的服务器控件,则在回发时执行 javascript

c# - 类、结构或接口(interface)成员声明中的标记无效

javascript - 在 Angular 服务中为 UI 缓存计算数据?

javascript - 如何使用 Google App Engine 作为后端在 Chrome 扩展中实现用户身份验证?

javascript - 如何用 div 包装一组匹配的 html

c# - 构建 NuGet 包时如何向函数添加文档

c# - nunit 根本不能在 Windows 7 中工作

c# - Nuget 和 "Unable to load the service index"

c# - 使用 JavaScriptSerializer 序列化 C# 对象会在 UTF8 字符串中产生无效字节

c# - 如何防止上传 Warez。 (asp.net, C#)