JavaScript Function Definition in ASP User Control [duplicate]

时间秒杀一切 提交于 2020-01-11 05:41:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!