无法从代码隐藏调用 JavaScript 函数
我正在 ASP.Net 环境中构建应用程序,我想从 C# 后面的代码中调用 JavaScript 函数。
我尝试使用 ScriptManager.RegisterStartupScript() 方法,它遇到错误,提示“当前内容中不存在名称“ScriptManager””,我已经包含命名空间 System.Web.UI 但仍然收到此错误。
知道这是怎么发生的吗?或者有更好的解决方法,而不是使用 ScripManager 从代码隐藏触发 JavaScript 函数?
提前谢谢您。
I'm working in application build in ASP.Net environment, I wanted to call JavaScript Function from the code behind in C#.
I tried using ScriptManager.RegisterStartupScript() method, it hit error saying "The name 'ScriptManager' does not exist in the current contect", I already include the namespace System.Web.UI and yet still got this error.
Any idea how can this happen? or there is a better workaround rather that using ScripManager to trigger JavaScript function from code behind?
thank you in advanced.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 MSDN 上阅读有关 RegisterStartupScript 的更多信息 http://msdn.microsoft.com/en- us/library/asz8zsxy.aspx
另外请确保页面上有一个表格。如果页面上没有表单,则脚本将无法执行。
Read more here on MSDN about RegisterStartupScript http://msdn.microsoft.com/en-us/library/asz8zsxy.aspx
Also make sure that you have a FORM on the page. If there is no form on the page then the script will fail to execute.
你的问题不清楚。您想在执行后台代码时同时调用 JavaScript 函数吗?如果是这样的话那就不可能了。
JavaScript 在客户端运行,后台代码在服务器端运行。控件要么是客户端,要么是服务器端,不能同时在客户端和服务器端。
ScriptManager.RegisterStartupScript
的作用是,在浏览器上加载文档时,它会在页面底部添加一个脚本块。这将允许您从页面的 javascript 代码调用新添加的 javascript 代码。这允许您在后端创建自定义 JavaScript 代码并将其推送到页面加载。如果还有其他情况,请澄清。
Your question is not clear. Do you want to call a javascript function simultaneously when the code behind code is executing? If that is the case then it is NOT possible.
Javascript runs on the client side and code behind runs on the server side. A control is either client side or server side, it cannot be simultaneously at both the sides.
What
ScriptManager.RegisterStartupScript
does is that at the time of document load on your browser it add a block of script at the bottom of your page. This will allow you to call that newly added javascript code from the javascript code of your page. This allows you to create custom javascript code at the backend and push it to page load.If there is some other scenario then please clarify.
试试这个,
Try this,