Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Elegant way of including large amounts of dynamic JavaScript?

Status
Not open for further replies.

NeilTrain

Programmer
May 27, 2003
275
US
Ok, since I use repeaters and other types of controls that rename controls on the client side, and since I also have a lot of javascript that calls on these controls, I have to output the javascript using controlName.ClientID all over the place to ensure that the controls are named properly.

The problem is my .cs page is full of these large concantenaded strings of JS that get added to the page via the ClientScriptBlock feature.

My js is cluttered and hard to debug because of this. Is there a way I can have something like a .js file, unencumbered by the surrounding C# code, and have it included dynamically, while preserving the ability to dynamically load the control names at runtime?
 
YOu can have .js files in your project and reference them from code.
 
In the HTML of the of the Page:
Code:
Ex:

<script language="javascript" type="text/javascript" src="/js/Forms.js"></script>
 
I know you can do that, however thats not solving my problem.

in a static .js file I cannot dynamically set control names as i do now.

for eaxmple, I currently output js using control.ClientID (which can not be known until runtime) in the C# code
 
If it's dynamic then it's dynamic. No way to encapsulate it nicely.
 
Well, you can have all of the javascript in single location instead of peppered all over the place (depending on exactly what you want to do).

You can, for instance, use a StringBuilder to build all of your javascript the call Page.ClientScript.RegisterStartupScript on the PreRender event, for example.

Can you post a small simplified page with your javascript statements so that it's easier to see what the problem is and what you want to accomplish?

MCP, MCTS - .NET Framework 2.0 Web Applications
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top