我想我总结了标题中的问题。这里有一些进一步的阐述...
我有一个 Web 用户控件,可以在多个地方使用,有时在给定页面上使用多次。
Web 用户控件具有一组特定的 JavaScript 函数(主要是 jQuery 代码),这些函数包含在 *.js 文件中并自动插入到页面标题中。
但是,当我想在页面上多次使用该控件时,*.js 文件会被包含“n”次,因此,浏览器会混淆它应该在哪个控件上执行哪个函数。
我需要做什么才能解决这个问题?我一整天都盯着这个,我很茫然。
非常感谢所有评论。
贾森
I think I summed up the question in the title. Here is some further elaboration...
I have a web user control that is used in multiple places, sometimes more than once on a given page.
The web user control has a specific set of JavaScript functions (mostly jQuery code) that are containted within *.js files and automatically inserted into page headers.
However, when I want to use the control more than once on a page, the *.js files are included 'n' number of times and, rightly so, the browser gets confused as to which control it's meant to be executing which function on.
What do I need to do in order to resolve this problem? I've been staring at this all day and I'm at a loss.
All comments greatly appreciated.
Jason
发布评论
评论(3)
如果问题只是同一文件被嵌入多次并导致冲突,请考虑使用
RegisterClientScriptInclude
。如果您对 RegisterClientScriptIninclude 的所有调用使用相同的标识符,则只会嵌入该文件的一个实例:http://msdn.microsoft.com/en-us/library/2552td66.aspx
但是,如果问题是您的方法正在被调用,但他们不知道对然后你需要弄清楚如何为你的 JS 提供一些上下文。在客户端定义一个代表您的控件的 JavaScript 对象,并在服务器端发出调用,该调用将使用您将要操作的控件的客户端 ID 来实例化该对象。
If the issue is simply that the same file is being embedded multiple times and causing conflict, look into using
RegisterClientScriptInclude
. If you use the same identifier for all of your calls to RegisterClientScriptInclude only one instance of that file will be embedded:http://msdn.microsoft.com/en-us/library/2552td66.aspx
However, if the issue is that your methods are being called but they don't know what controls on the page to operate on then you need to figure out how to provide your JS some context. Define a JavaScript object that represents your control on the client-side, and on the server-side emit the call that will instantiate it with the client IDs of the controls you'll be operating on.
我们使用
CustomValidator
来验证用户控件。该控件工作正常,直到您将该控件的两个实例放在同一页面上,因为它们引用完全相同的 JavaScript 函数,因此只有一个控件可以工作。为了解决这个问题,我们在 JavaScript 函数名称后附加了控件 ID。在代码隐藏中,我们分配了
ClientValidationFunction
这可能不是正确的方法,但它有效。
We are using
CustomValidator
to validate User Control. The control works fine until you drop two instances of the Control on the same page, since they reference the exact same JavaScript functions, only one control works. Work around, we appended JavaScript function name with control id.In codebehind, we assinged
ClientValidationFunction
This may not be the right approach but it works.
我以前也遇到过这种情况。您可以使用 ScriptManager 在页面中注册一个单独的 JavaScript 文件。如果您愿意,可以将其作为嵌入到 dll 中的资源文件进行流式传输。
然后您只需从您的控件中调用函数即可。
否则,完全独立的 jquery 文件也可以工作。
I've had this situation before. You register a separate JavaScript file with the page using the ScriptManager. You can stream this as a resource file embedded into the dll if you wish.
Then you only call into the functions from your control.
Otherwise a completely separate jquery file may also work.