0

I have created a Web Control in ASP for use in integrating with Telligent CommunityServer. The control is written in ASP with some 10 lines of C# backend for controlling visibility of the UI elements based on permissions, but I'd say 90% of the functionality is straight-up Javascript.

The control works beautifully, until you drop two instances of the Control on the same page--since they reference the exact same Javascript functions, only one control works.

How can I take this functionality that I have, this 1200 lines of Javascript, and make it so that each instance of the control can reference its each unique instance of Javascript?

3
  • We need to see some sample code, because as written the question doesn't make much sense. Commented May 10, 2010 at 23:22
  • Well, there's way too much code to sample... I have created an ASCX Web Control that contains Javascript. When I put two of these ASCX files on an ASPX Page, there is duplicated Javascript. For example, they both reference a Javascript Load() function, yet that Load() function doesn't know which Control to load. Make sense? Commented May 10, 2010 at 23:26
  • Is this a widget for TC5+ or just a control for another version of Telligent? Commented May 11, 2010 at 11:51

2 Answers 2

1

If the problem is simply finding out which HTML element triggered an event then that's easy - it's passed to you in the event arguments. (You can then get its parent element, of course.) So if I understand the problem correctly you have JavaScript functions referencing some controls, which get duplicated?

I don't think there's any magic solution here: you just have to make the IDs unique. If you're not using server-side controls (which handle this for you automatically) then you could use some server-side generated prefix or suffix, eg.

<input id="<%= ParentControlId %>_mytextbox" type="text" />

where ParentControlId is a unique ID of the parent control (you could just use Control.ID if you don't mind long IDs).

Of course, you should try to re-use your JavaScript functions rather than duplicating them for each control instance. If you're using the server-generated ID approach you'll have to pass entire IDs around. If you're using the prefix approach you can just pass around the prefix.

Sign up to request clarification or add additional context in comments.

Comments

0

How is your control getting the JavaScript into the page, then? Injecting it directly? Are you using a ScriptReference? The browser would only load the JS once if both controls referenced that same JS (same URL).

Also, is your JS written in terms of any old instance? e.g. do you have function like Load(oControlName) or does the Load() function have a hard-coded instance name inside it?

IMO, trying to get multiple 'JavaScripts' is not the way forward - refactoring your code may get your answer quicker.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.