在自定义服务器控件中嵌入 javascript 资源
我正在基于我在网上找到的 jQuery 增强文本框创建我的第一个 ASP.NET 服务器控件。部分原始代码包含 jQuery 扩展文件,因此我需要将其包含在我的控件中。 (基本 jQuery 库是 Web 应用程序的要求,因此出于我们的目的,我们可以放心地假设它已经在使用控件的任何页面上可用。)
我首先将 javascript 文件添加为资源,并能够在其中写入流控件的渲染事件。虽然它根据需要创建了 HTML/javascript,但 jQuery 并不能正常工作。
我遇到的第二个问题是我预计在任何给定页面上都会使用该控件的多个实例,因此即使上述方法有效,该 javascript 也会不断重复。恶心。
如何在我的控件中包含此 javascript,但确保仅呈现它的单次迭代?谷歌搜索发现了一些关于使用 as [assemble]
属性和 Page.RegisterClientScriptResource
的引用,但我对此不太清楚。
I'm creating my first ASP.NET server control based on a jQuery-enhanced textbox I found online. Part of the original code includes a jQuery extension file, so I need to include that with my control. (The base jQuery library is a requirement of the web application so for our purposes here we can safely assume it's already available on any page the control is used.)
I first added the javascript file as a resource and was able to write the stream in the control's Render event. While it created the HTML/javascript as wanted, the jQuery doesn't work quite right.
The second problem I have is I anticipate several instances of the control to be used on any given page, so even if the above method worked, that javascript would be constantly repeated. Yuck.
How can I include this javascript with my control but ensure only a single iteration of it is rendered? Googling has found some references to using as [assembly]
attribute and Page.RegisterClientScriptResource
, but I'm not too clear on that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
RegisterClientScriptResource 的全部目的是防止页面重新加载重复的脚本。
请注意,上面的示例假设您正在 Intranet 上工作。如果您使用公共网站,最好使用 CDN 托管文件 (http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js)
The entire point of the RegisterClientScriptResource is to prevent the page from reloading a duplicate script.
Note that the above example assumes you are working on an Intranet. If you are working with a public website, it is better to use CDN Hosted file (http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js)
一种想法是将其嵌入页面中,然后将其用作:
另一种想法是添加
cache-control
指令。这将导致浏览器从本地缓存加载它以供后续请求。WebResources
在发布模式debug=false
下构建时会发出cache
指令。One idea is to embed it in a page and then serve it as:
Another is to add
cache-control
directives. This will cause the browser to load it up from its local cache for subsequent requests.WebResources
emitcache
directives when they are built in release modedebug=false
.