JavaScript function definition in ASP User Control

Possible duplicate:
Custom javascript

I defined a JavaScript function inside a user control.

If I have multiple instances of a user control on my .aspx page, how can I prevent a lot of function definitions in the resulting HTML?

+4
source share
2 answers

You will want to use the Page.ClientScript manager.

The following code will only register your code once per page.

if (!page.ClientScript.IsClientScriptBlockRegistered(tType, "MyScript")) { page.ClientScript.RegisterClientScriptBlock(tType, "MyScript", sScript); } 

You can also make sure that * .js files are added only once

 if (!page.ClientScript.IsClientScriptIncludeRegistered(tType, "MyScriptFile")) { page.ClientScript.RegisterClientScriptInclude(tType,"MyScriptFile","MyJavaScript.js") } 
+2
source

Could you put the javascript code in a separate .js file and link to the common.js file from a web page or main page?

+1
source

Source: https://habr.com/ru/post/1306391/


All Articles