使用 WSPBuilder 将 JavaScript、CSS 等添加到 WebPart
所以,我试图做我认为是一项简单的任务......但我没有取得任何进展...... 我想要的只是让我的 WebPart 加载一些 .js 和 .css 文件。我正在使用 VS2008 + WSPBuilder。我在谷歌上搜索了很多关于这个问题的信息,但找不到合适的答案。 我想知道的是:
-
我应该将这些文件放置在目录结构中的哪个位置? (例如,12/TEMPLATE/OTHER?80/wpresources/ assembly_name?)
-
如何访问这些文件? (使用相对路径?通过某种方法获取完整路径?)
-
最后,如何将这些文件添加到页面的
?
预先感谢..我在这些问题上浪费了整个上午的时间,我正在考虑改变职业! ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,经过两支烟和更有效的谷歌搜索后,我找到了解决方案。希望这可以帮助那些和我有同样麻烦的人!
这些额外文件的正确位置位于
12/TEMPLATE/LAYOUTS/1033/yourapp/
如果它们位于此位置,则这些文件的路径为
/_layouts/1033/yourapp/yourjs.js
如果您想将 JavaScript 文件添加到页面的头部,请将此代码放在 CreateChildControls 中()方法:
如果要将 CSS 文件添加到页面的头部,请将此代码放入 CreateChildControls() 方法中:
好吧,希望您发现这很有帮助!
Ok, after two cigarettes and more efficient google searches I found the solution.. Hope this helps someone in the same trouble as I was!
The right place to those extra files is in
12/TEMPLATE/LAYOUTS/1033/yourapp/
If they are in this place, the path to those files is
/_layouts/1033/yourapp/yourjs.js
If you want to add a JavaScript file to the head of the page, place this code in the CreateChildControls() method:
If you want to add a CSS file to the head of the page, place this code in the CreateChildControls() method:
Well, hope you found this helpfull!
资源应位于 wpresources 文件夹中。
如果管理员将您的 Web 部件部署到 BIN 目录,则此文件夹将位于类似的位置,该文件夹
将映射到类似的位置
如果管理员部署到 GAC,则它将
映射到
要找出运行时所需的路径,您可以应使用 WebPart.ClassResourcePath
因此,修改您的代码
MSDN - 创建带有客户端脚本的 Web 部件
Resources should go in the wpresources folder.
If an admin deploys your web part to the BIN directory then this folder will be in something like
which will be mapped to something like
If an admin deploys to the GAC then it will be
mapped to
To find out the path that you need at runtime you should use WebPart.ClassResourcePath
So modifying your code
MSDN - Creating a Web Part with Client-side Script
我知道这是一篇旧文章,但是有更好的方法来做到这一点。这种方式要好得多,因为它可以将 js 和 CSS 文件保留在项目本地。我发现如果可能的话最好远离 12 个配置单元,并且通过添加 CSS 和 js 文件,这是可能的。
首先,您应该在 C# 项目的顶层创建一个文件夹来放置 js 文件和 CSS 文件。将 js/CSS 文件放入此文件夹中。
接下来,您将创建一个 Constants.cs 类来调用您的 js 或 CSS 文件。这是该类的代码。
请
注意,在代码中它重新指向 Resources 文件夹。这是我在第一步中创建的额外文件夹。你想怎么称呼它就怎么称呼它。
下一部分是将您对 js 文件/CSS 的引用添加到程序集中。
完成此操作后,您就可以在 WSP 项目的主 C# 类中调用和使用 js/CSS 了。
CSS 文件有点不同
我发现这是调用 js 和 CSS 文件的最好、最可靠的方式。这就是我为所有 Web 部件加载 JavaScript 和 CSS 文件的方式。我这样做从来没有遇到过问题。我希望这可以帮助一些遇到此问题的人。 SharePoint 永远不会让任何事情变得简单。 :)
I know this is an old post, however there is a better way to do this. This way is much better because it keeps your js and CSS files local to your project. I have found that it is best to stay out of the 12 hive if at all possible, and with adding CSS and js files, it is possible.
First, you should make yourself a folder to place your js files and CSS files in at the top level of your C# Project. Place your js/CSS files into this folder.
Next you are going to make a Constants.cs Class that will call your js or CSS files. here is the code for the class.
}
Notice that in the code it is refurring to the Resources folder. This is the extra folder that I made in the first step. Call it what you want.
Next part is adding your reference to you js file/CSS to the assembly.
After this is done, you are ready to call and use your js/CSS in the main C# class of your WSP project.
CSS files are a little different
I have found that this is the best and most reliable way to call js and CSS files. This is how I load my JavaScript and CSS files for all of my web parts. I have never had an issue with doing it this way. I hope this helps out some people who are having problems with this. SharePoint never makes anything easy. :)
我在使用 lazoDev 的解决方案时遇到了一些问题(拼写错误、缺少引用以及编码错误),我想分享一下我的解决方案。
第 1 步:名为“日历”的 Web 部件
我的样式位于这样的 Styles 文件夹中:
ProjectName/Styles/
而脚本位于 Scripts 文件夹中:
ProjectName/Scripts/
步骤 2:AssemblyInfo.cs
步骤 3:文件属性
转到每个脚本和样式文件的属性,并将
Build Action
的值更改为Embedded资源
。I had some problems getting lazoDev's solution to work (spelling errors, missing references, as well as a coding error) and thought I'd share my solution.
Step 1: Webpart called "Calendar"
My styles are locating in a Styles folder like this:
ProjectName/Styles/
While the scripts are located in a Scripts folder:
ProjectName/Scripts/
Step 2: AssemblyInfo.cs
Step 3: File Properties
Go to the properties of each script and style file and change the value of
Build Action
toEmbedded Resource
.