开发者

JavaScript Function Definition in ASP User Control [duplicate]

开发者 https://www.devze.com 2022-12-26 08:33 出处:网络
This question already has answers here: Closed 11 ye开发者_如何学JAVAars ago. Possible Duplicate:
This question already has answers here: Closed 11 ye开发者_如何学JAVAars ago.

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?


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


Could you put the javascript code in a separate .js file and reference that common.js file from the web page or master page?

0

精彩评论

暂无评论...
验证码 换一张
取 消