asp.net 服务器控件 - 避免添加多个 javascript
我创建了一个派生自 LinkButton 的 asp.net 服务器控件,并向页面呈现一个小的 javascript 函数。
我想在页面上多次使用此控件,但只想在页面上渲染一次 javascript。另一方面,我宁愿不手动将其添加到js文件中,因为我不想将来有机会忘记添加js文件。
这样做的最佳方法是什么?
I created an asp.net Server control that derives from a LinkButton, and renders a small javascript function to the page.
I want to use this control many times on the page, but just want the javascript to be rendered once on the page. On the other hand, I rather not manually add it in a js file, because i don't want to have a chance of forgetting to add the js file in the future.
What will be the best way to go about doing this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
防止客户端脚本中出现重复
使用注册方法,例如 Page.ClientScript.RegisterClientScriptBlock(..)。有一些重载,但它们都以相似的方式工作。它足够聪明,无论发出多少个控制实例,都只渲染一次脚本。
Microsoft MSDN 文档中的注释:
MSDN 文档还包含示例代码,如下所示:
其他类似的注册方法
可以使用不同的方法以不同的方式注册您的客户端脚本 - 请参阅每个 ClientScriptManager.RegisterClientScript*(..) 中的方法MSDN 了解有关每个内容的更多详细信息。它们都是为了防止渲染页面中出现重复而构建的。
这使您可以自由地在控制级别输出脚本,而不必担心或不必跟踪或计数脚本。
编辑:
ClientScriptManager 实例通过 Page.ClientScript 属性。
Preventing duplication in client scripts
Use a registration method such as Page.ClientScript.RegisterClientScriptBlock(..). There are a couple of overloads but they all work in a similar fashion. It's smart enough to render the script only once no matter from how many control instances it's issued.
Microsoft remarks in the MSDN documentation:
The MSDN docs also contain sample code such as this:
Other similar registration methods
Different methods can be used to register your client script in different ways - see each of the ClientScriptManager.RegisterClientScript*(..) methods in MSDN for more detail about each. They're all built to prevent duplication in the rendered page.
This gives you the freedom to output scripts at the control level without worrying or having to track or count scripts.
Edit:
The ClientScriptManager instance is accessed through the Page.ClientScript property.
您可以使用 Page.RegisterClientScriptBlock 方法。
这是来自 MSDN 的示例:
HTH
You can use the Page.RegisterClientScriptBlock method.
Here is an example from the MSDN:
HTH
有什么方法可以引用 CDN 上的脚本而不使其重复多次吗?或者我可以不担心它,因为它只是一个指针,并且浏览器(?)足够聪明,不会加载同一个库两次?
换句话说,假设我希望从服务器控件插入以下代码:
如果我使用 RegisterClientScriptBlock,我可以使用 IsClientScriptBlockRegistered 方法来避免服务器控件多次加载它。但是,前面的代码很可能是由另一个(不同的)服务器控件或想要将 jQuery 用于服务器控件之外的内容的开发人员放在页面上的。
如果开发人员已经加载了脚本,如何让服务器控件不加载脚本?
Any way to reference a script on a CDN and not have it duplicated multiple times? Or can I just not worry about it since it's just a pointer anyway and the browser is (?) smart enough not to load the same library twice?
In other words, suppose I want the following code inserted from a server control:
If I use a RegisterClientScriptBlock I can use the IsClientScriptBlockRegistered method to not have the server control load it multiple times. But, the preceding code is likely to have been put on the page anyway by either another (different) server control or by the developer who wants to use jQuery for something outside of the server control.
How do I get the server control to not load the script if the developer already loaded it?