Multiple user controls and javascript

I am including a JS file in a user control. There are several instances of the user control on the main page.

The js file has a global variable that is used as a flag for the js function. I need the scope of this variable to be limited to the user control. Unfortunately, when I have multiple instances of the control, the value of the variable is overwritten.

What is the recommended approach in this situation?

+6
javascript
source share
4 answers

I would recommend refactoring your code so that all of the common JS logic is stored in one place, and not in every UserControl. This will significantly reduce the size of your page.

You can pass the UserControl identifier to a common JS method to distinguish between UserControls.

To limit the scope of your UserControl variable, you can save some Key / Value structure to save your UserControl value - the key will be the UserControl client identifier, and the value you are interested in.

For example:

var UCFlags = new Object(); //set the flag for UserControl1: UCFlags["UC1"] = true; //set the flag for UserControl2: UCFlags["UC2"] = false; 

To access them, you simply pass the ClientID UserControl identifier to the UCFlags array:

 myFlag = UCFlags["UC1"]; 

On the server side, you can replace the constant string "UC1" or "UC2" with

 <%= this.ClientID %> 

like this:

 myFlag = UCFlags["<%= this.ClientID %>"]; 

You can still use the syntax <% = this.ClientID%>, even if the bulk of the JS is in a separate file; just install

 UCFlags["<%= this.ClientID %>"] = value; 

before calling to embed the js file.

+2
source share

Some options are the dynamic creation of javascript based on ClientId User Control. For example, you can dynamically generate a global variable.

Another option and one that I would recommend is to encapsulate a global variable and function inside an object, then your user control can emit JS to instantiate this object (which can be dynamically named, allowing you to span the object as you see fit) .

Edit

I do not have a sample working code that I can use, but I did this in several ways. The easiest way is to do this in the layout of your user control.

 <script language='javascript'> var <%=this.ClientID%>myObject=new myObject(); </script> 

Assuming your control has a clientId myControl, this will create the myControlmyObject variable.

Another way to do this is to create a script in code that you could register using: Page.ClientScript.RegisterStartupScript() .

+5
source share

Well, if you need to support the current solution, you can rename your global variable to something like the following code, which should be in the .ascx file for your control:

 <script type='text/javascript'> var <%= this.ClientID %>_name_of_global_variable; </script> 

Where "this" is an asp.net control. Thus, each control has a unique variable name based on the client identifier. Make sure you update the rest of your javascript to use this new naming convention. The problem is, it looks messy, and the variable names become very long depending on where the control is embedded in the page.

It makes sense? It requires minimal javascript modification to work.

+2
source share

I ran into the same issue and below blog posts resolved it. The solution is to use an object oriented path for javaScript

Adding multiple .NET custom controls that use JavaScript on the same page

0
source share

All Articles