问题
Possible Duplicate:
User control javascript
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?
回答1:
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")
}
回答2:
Could you put the javascript code in a separate .js file and reference that common.js file from the web page or master page?
来源:https://stackoverflow.com/questions/2602398/javascript-function-definition-in-asp-user-control