在自定义服务器控件中嵌入 javascript 资源

发布于 2024-11-24 05:57:24 字数 476 浏览 3 评论 0原文

我正在基于我在网上找到的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨落画卷 2024-12-01 05:57:24
Page.ClientScript.RegisterClientScriptResource(getType(page), _
   page.ResolveUrl("~/scripts/jquery.js")

RegisterClientScriptResource 的全部目的是防止页面重新加载重复的脚本。

请注意,上面的示例假设您正在 Intranet 上工作。如果您使用公共网站,最好使用 CDN 托管文件 (http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js)

Page.ClientScript.RegisterClientScriptResource(getType(page), _
   page.ResolveUrl("~/scripts/jquery.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)

少女七分熟 2024-12-01 05:57:24

一种想法是将其嵌入页面中,然后将其用作:

const string JAVASCRIPT_RESOURCE = "[namespace].[class].customJS.js"
Stream st = Assembly.GetExecutingAssembly().GetManifestResourceStream(JAVASCRIPT_RESOURCE);
StreamReader sr = new StreamReader( st );
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), 
                                             "customJS", sr.ReadToEnd(), true);

另一种想法是添加 cache-control 指令。这将导致浏览器从本地缓存加载它以供后续请求。 WebResources 在发布模式 debug=false 下构建时会发出 cache 指令。

One idea is to embed it in a page and then serve it as:

const string JAVASCRIPT_RESOURCE = "[namespace].[class].customJS.js"
Stream st = Assembly.GetExecutingAssembly().GetManifestResourceStream(JAVASCRIPT_RESOURCE);
StreamReader sr = new StreamReader( st );
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), 
                                             "customJS", sr.ReadToEnd(), true);

Another is to add cache-control directives. This will cause the browser to load it up from its local cache for subsequent requests. WebResources emit cache directives when they are built in release mode debug=false.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文