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") }
source share