同一ASP.NET用户的多个实例控制Javascript名称冲突

时间:2022-06-25 09:26:08

Problem: I have a user control (.ascx) with a ton of javascript code in there. I have two instances of the user control on the same page. When the code runs the two user controls behave erratically because all the js objects with the same name can only have one value (including functions). The javascript is in the code behind and it is registered via "ScriptManager.RegisterStartupScript(.....)".

问题:我有一个用户控件(.ascx),里面有大量的javascript代码。我在同一页面上有两个用户控件实例。当代码运行时,两个用户控件的行为不正常,因为具有相同名称的所有js对象只能有一个值(包括函数)。 javascript在后面的代码中,它通过“ScriptManager.RegisterStartupScript(.....)”注册。

I've searched thoroughly and although there are a few threads with suggestions I found nothing that works other than literally going through the whole user control and appending an ID to function names that raise collision problems. (which requires meticulously going through all the code and deterimining if it is problematic).

我已经彻底搜索过了,虽然有一些建议的线程我发现除了字面上通过整个用户控件并将ID附加到引发碰撞问题的函数名称之外没有其他任何工作。 (这需要仔细检查所有代码并确定是否存在问题)。

example:

例:

Dim asdf As String = ""
asdf &= "function foo" & UNIQUE_ID & "() {" & vbCrLf
asdf &= .....u get the idea......
asdf &= "}"

Is there a more elegant solution than this? Is it just bad practice to put javascript inside user controls for this reason?

有比这更优雅的解决方案吗?出于这个原因,将javascript放在用户控件中是不是不好的做法?

1 个解决方案

#1


1  

Maybe try to close all the JS code of a single control in a namespace? So the resulting code could look something like this:

也许尝试关闭命名空间中单个控件的所有JS代码?所以生成的代码看起来像这样:

var controlNamespace = {
   ...here goes the code that you have now for a control
};

You'll be able to call your functions by adding "controlNamespace." prefix. The only thing you need to care about then is that each control's script is enclosed in different namespace. Which should be pretty easy, assuming that you crete all the JS code dynamically on the server side, like in the example you pasted. This way every control will have it's corresponding code enclosed in a separate namespace and they won't interfere with themselves no matter how many of these controls you create on a page.

您可以通过添加“controlNamespace”来调用您的函数。字首。您唯一需要关心的是每个控件的脚本都包含在不同的命名空间中。假设您在服务器端动态创建所有JS代码,就像在您粘贴的示例中一样,这应该很简单。这样每个控件都会将它的相应代码包含在一个单独的命名空间中,无论您在页面上创建了多少这些控件,它们都不会干扰它们自己。

#1


1  

Maybe try to close all the JS code of a single control in a namespace? So the resulting code could look something like this:

也许尝试关闭命名空间中单个控件的所有JS代码?所以生成的代码看起来像这样:

var controlNamespace = {
   ...here goes the code that you have now for a control
};

You'll be able to call your functions by adding "controlNamespace." prefix. The only thing you need to care about then is that each control's script is enclosed in different namespace. Which should be pretty easy, assuming that you crete all the JS code dynamically on the server side, like in the example you pasted. This way every control will have it's corresponding code enclosed in a separate namespace and they won't interfere with themselves no matter how many of these controls you create on a page.

您可以通过添加“controlNamespace”来调用您的函数。字首。您唯一需要关心的是每个控件的脚本都包含在不同的命名空间中。假设您在服务器端动态创建所有JS代码,就像在您粘贴的示例中一样,这应该很简单。这样每个控件都会将它的相应代码包含在一个单独的命名空间中,无论您在页面上创建了多少这些控件,它们都不会干扰它们自己。