简体   繁体   English

C#中的Javascript和用户控件

[英]Javascript and User Controls in C#

I have some JS functions within the ASCX of a user control. 我在用户控件的ASCX中有一些JS函数。 This UC is loaded multiple times in the host page. 在主机页面中多次加载了此UC。 When I do a view source, I see the JS repeated for every instance of the UC. 当我执行视图源时,我看到对于UC的每个实例重复执行JS。

Is there a way I can have it load the functions only once? 有没有办法让我只加载一次函数? I would like to reduce the size of the page for better performance 我想缩小页面尺寸以获得更好的性能

Put the functions in a separate .js file, then add it by calling Page. 将函数放在单独的.js文件中,然后通过调用Page添加它。 ClientScript . ClientScript RegisterClientScriptInclude (). RegisterClientScriptInclude ()。

You can also mark the file as an Embedded Resource, and then include it using Page.ClientScript. 您也可以将文件标记为“嵌入式资源”,然后使用Page.ClientScript将其包括在内。 RegisterClientScriptResource (). RegisterClientScriptResource ()。

Note that if you're using a ScriptManager/UpdatePanels, use ScriptManager . 请注意,如果您使用的是ScriptManager / UpdatePanels,请使用ScriptManager RegisterClientScriptInclude or . RegisterClientScriptInclude或。 RegisterClientScriptResource so that it'll know how to add the reference correctly on a partial postback. RegisterClientScriptResource,以便它将知道如何在部分回发中正确添加引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM