I defined a JavaScript function inside a user control.
If I have multiple instances of the user control on my .aspx page, how do I prevent multiple function definitions in the resulting HTML code?
I defined a JavaScript function inside a user control.
If I have multiple instances of the user control on my .aspx page, how do I prevent multiple function definitions in the resulting HTML code?
Could you put the javascript code in a separate .js file and reference that common.js file from the web page or master page?
You'll want to use the Page.ClientScript manager.
The following code will only register your code once per a page.
if (!page.ClientScript.IsClientScriptBlockRegistered(tType, "MyScript"))
{
page.ClientScript.RegisterClientScriptBlock(tType, "MyScript", sScript);
}
You can also make sure that *.js files get added only once
if (!page.ClientScript.IsClientScriptIncludeRegistered(tType, "MyScriptFile"))
{
page.ClientScript.RegisterClientScriptInclude(tType,"MyScriptFile","MyJavaScript.js")
}