Ideas for creating a unique javascript object name in ASP.Net?

I have created a custom ASP.Net control that will be hosted multiple times inside a web page. In this control, I have defined a javascript object, for example:

function MyObject( options )
{
  this.x = options.x;
}

MyObject.prototype.someFunction=function someFunctionF()
{
  return this.x + 1;
}

      

In the code behind, I have created MyObject in the startup script -

var opts = { x: 99 };

var myObject = new MyObject( opts );

      

When some button in the control is clicked, myObject.someFunction () is called. Now let's say that x will be 99 for one control and 98 for another control. The problem here is that var myObject will iterate and only the last instance will matter. Of course, there is a way to make var myObject unique using some concept that I haven't encountered yet. Ideas?

Thank,

Craig

0


source to share


5 answers


Your Javascript is like this: -

function MyObject(options) { this.x = options.x; }
MyObject.prototype.someFunction = function() { return this.x + 1; }
MyObject.create(id, options) {
    if (!this._instances) this._instances = {};
    return this._instances[id] = new MyObject(options);
}
MyObject.getInstance(id) { return this._instances[id]; }

      

Your javascript to run looks like this: -

MyObject.create(ClientID, {x: 99});

      



Other code that should use the instance (for example in the client side onclick event)

String.Format("onclick=\"MyObject.getInstance('{0}').someFunction()\", ClientID);

      

Note the low impact on the global client namespace, only the MyObject identifier is added to the global namespace, no matter how many instances of your control are added to the page.

+1


source


If it's just one value, why not use a function as a parameter and create an onclick handler so that it fills in the correct value for each control. If it is more complex, then consider creating array parameters and for each control, insert the correct parameters into the spot in the array corresponding to each specific control. Then pass the appropriate array index to the function.



0


source


I do this using ScriptManager.RegisterClientScriptBlock to register the string as a client side JavaScript block. Then I can change the script line using {0}, {1} .., {n} space holders to enter the required IDs. It depends on the structure of your code, if this is the most elegant way, but it works as a last resort. Then you can enter variable names using Me.ClientID references.

0


source


You can make a static value "x" and access it anywhere in your code, for example:

function MyObject( options ) { MyObject.x = options.x; }
MyObject.x = 99; // static
MyObject.prototype.someFunction = function () { return MyObject.x + 1; }

      

This way, you can access MyObject.x anywhere in your code, even without re-running MyObject.

0


source


Great solution from Anthony. Other suggested solutions were just as good and I considered them, but I was looking for something more elegant like this solution. Thank!

0


source







All Articles