usercontrol中的javascript无法正常工作 [英] javascript in usercontrol not working

查看:91
本文介绍了usercontrol中的javascript无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户控件:
在按钮上单击即可调用Java脚本功能
javascript函数

User control:
on button click call java script function
javascript function

function clear() {
    alert('test');
}





and

btn.Attributes.Add("onclick", "clear()");



在页面上:
我的页面上有2个用户控件

问题:
单击按钮上的脚本不起作用



on page:
i had 2 user controls on my page

problem:
on button click script doesn''t work

推荐答案

在两个用户控件实例中,您都有一个具有相同名称的javacript函数.因此在该页面上,您实际上最终得到了两个具有相同名称的函数,这导致了问题.

我建议在函数后添加控件ID,以使函数名称不同.

另外,我认为您需要使用RegisterClientScript 从服务器端编写这些功能,以使其起作用.
You have a javacript function with same name in both the instances of user control. so on the page you actually ended up with the 2 functions with same name and this is causing the problem.

I suggest to add the controls ID after the function so that the function names are different.

Also, I think you need to write these functions from server side using RegisterClientScript for them to work.


如果您添加了单独的JavaScript文件,则添加了 reference 该文件到您的页面.
If you added Separate JavaScript File then add reference of that file to your page.


这篇关于usercontrol中的javascript无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆