关于在 ASP.Net 中使 javascript 对象名称唯一的想法?
我创建了一个 ASP.Net 用户控件,它将多次放置在网页内。 在此控件中,我定义了一个 javascript 对象,例如:
function MyObject( options )
{
this.x = options.x;
}
MyObject.prototype.someFunction=function someFunctionF()
{
return this.x + 1;
}
在后面的代码中,我在启动脚本中创建了 MyObject ——
var opts = { x: 99 };
var myObject = new MyObject( opts );
当按下控件中的某个按钮时,它将调用 myObject.someFunction()。 现在假设一个控件的 x 值为 99,而另一个控件的 x 值为 98。 这里的问题是 var myObject 将被重复,并且只有最后一个实例才重要。 当然有一种方法可以使用我还没有遇到过的一些概念来使 var myObject 独一无二。 有想法吗?
谢谢,
克雷格
I've created an ASP.Net user control that will get placed more than once inside of web page. In this control I've defined a javascript object such as:
function MyObject( options )
{
this.x = options.x;
}
MyObject.prototype.someFunction=function someFunctionF()
{
return this.x + 1;
}
In the code behind I've created MyObject in a startup script --
var opts = { x: 99 };
var myObject = new MyObject( opts );
When a certain button in the control is pressed it will call myObject.someFunction(). Now lets say the value of x will be 99 for one control but 98 for another control. The problem here is that the var myObject will be repeated and only the last instance will matter. Surely there's a way to make the var myObject unique using some concept I've haven't run across yet. Ideas?
Thanks,
Craig
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的 Javascript 如下所示:-
您的启动 javascript 如下所示:-
需要使用实例的其他代码(例如在客户端 onclick 事件中)
请注意,对客户端全局命名空间的影响较小,仅将 MyObject 标识符添加到全局命名空间命名空间,无论有多少控件实例添加到页面中。
Your Javascript like this:-
Your startup javascript like this:-
Other code that needs to use an instance (say in the client-side onclick event)
Note the low impact on the clients global namespace, only the MyObject identifier is added to the global namespace, regardless of how many instances of your control are added to the page.
如果它只是一个值,为什么不让函数将其作为参数并构建 onclick 处理程序,以便为每个控件放入正确的值。 如果比这更复杂,则考虑将选项设置为一个数组,然后对于每个控件,将正确的选项插入数组中与每个特定控件相对应的位置。 然后将数组中的正确索引传递给函数。
If it is just one value, why not have the function take it as a parameter and build your onclick handler so that it puts the correct value in for each control. If it is more complex than that, then consider making options an array and, for each control, insert the correct options into the spot in the array that corresponds to each particular control. Then pass the proper index into the array into the function.
我通过使用 ScriptManager.RegisterClientScriptBlock 在客户端将字符串注册为 JavaScript 块来实现此目的。 然后,我可以使用 {0}、{1}..、{n} 占位符修改脚本字符串以注入必要的 ID。 这是否是最优雅的方式取决于代码的结构,但它可以在紧要关头起作用。 然后,您可以使用对 Me.ClientID 的引用来注入变量名称。
I do this by using ScriptManager.RegisterClientScriptBlock to register a string as a JavaScript block on the client side. I can then modify my script string using {0}, {1}..,{n} place holders to inject necessary ids. It depends on the structure of your code as to if this is the most elegant fashion, but it works in a pinch. You could then inject variable names using references to Me.ClientID.
您可以将“x”的值设为静态并在代码中的任何位置访问它,例如:
这样您就可以在代码中的任何位置访问 MyObject.x,甚至无需重新实例化 MyObject。
You can make the value of "x" static and access it anywhere in the code, such as:
This way you can access MyObject.x anywhere in your code, even without re-instanciating MyObject.
优秀的解决方案安东尼。 提供的其他解决方案同样好,我确实考虑过它们,但我正在寻找像这个解决方案这样更优雅的东西。
谢谢!
Excellent solution Anthony. The other solutions offered were as good and I did consider them but I was looking for something a little more elegant like this solution.
Thanks!