问题 使用WSPBuilder将JavaScript,CSS和其他内容添加到WebPart


所以,我正在努力做我认为是一项简单的任务...但我没有得到任何地方...... 我想要的是获取我的WebPart加载的一些.js和.css文件。我正在使用VS2008 + WSPBuilder。我已经搜索了很多关于此的内容,但我找不到合适的答案。 我想知道的是:

  • 我应该在目录结构中放置那些文件? (例如12 / TEMPLATE / OTHER?80 / wpresources / assembly_name?)

  • 我该如何处理这些文件? (使用相对路径?通过某种方法获取完整路径?)

  • 最后,我如何将这些文件添加到页面中 <head>

在此先感谢..我在这些问题中失去了所有的早晨,我正在考虑改变职业生涯! ;)


8444
2018-03-11 15:51


起源



答案:


好吧,经过两次卷烟和更有效的谷歌搜索我找到了解决方案..希望这有助于遇到与我同样麻烦的人!

  • 这些额外文件的正确位置在 12/TEMPLATE/LAYOUTS/1033/yourapp/

  • 如果他们在这个地方,那么这些文件的路径是 /_layouts/1033/yourapp/yourjs.js

  • 如果要将JavaScript文件添加到页面的头部,请将此代码放在CreateChildControls()方法中:

    string scriptPath = "/_layouts/1033/yourapp/yourjs.js";
    if(!this.Page.ClientScript.IsClientScriptBlockRegistered(scriptPath))
    this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), scriptPath,
    "<script language=\"javascript\" src=\"" + scriptPath + "\">");
  • 如果要将CSS文件添加到页面的头部,请将此代码放在CreateChildControls()方法中:

    CssLink cssLink = new CssLink();
    cssLink.DefaultUrl = "/_layouts/1033/yourapp/yourcss.css";
    this.Page.Header.Controls.Add(cssLink);

好吧,希望你发现这有用!


9
2018-03-11 17:13



呃 - 不要以为这是对的。 - Ryan
不确定它是否是最佳方式......但它确实有效。 - Dave Brace


答案:


好吧,经过两次卷烟和更有效的谷歌搜索我找到了解决方案..希望这有助于遇到与我同样麻烦的人!

  • 这些额外文件的正确位置在 12/TEMPLATE/LAYOUTS/1033/yourapp/

  • 如果他们在这个地方,那么这些文件的路径是 /_layouts/1033/yourapp/yourjs.js

  • 如果要将JavaScript文件添加到页面的头部,请将此代码放在CreateChildControls()方法中:

    string scriptPath = "/_layouts/1033/yourapp/yourjs.js";
    if(!this.Page.ClientScript.IsClientScriptBlockRegistered(scriptPath))
    this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), scriptPath,
    "<script language=\"javascript\" src=\"" + scriptPath + "\">");
  • 如果要将CSS文件添加到页面的头部,请将此代码放在CreateChildControls()方法中:

    CssLink cssLink = new CssLink();
    cssLink.DefaultUrl = "/_layouts/1033/yourapp/yourcss.css";
    this.Page.Header.Controls.Add(cssLink);

好吧,希望你发现这有用!


9
2018-03-11 17:13



呃 - 不要以为这是对的。 - Ryan
不确定它是否是最佳方式......但它确实有效。 - Dave Brace


资源应该放在wpresources文件夹中。

如果管理员将您的Web部件部署到BIN目录,那么此文件夹将类似于

http://server/site/wpresources/YourWebPart/

它将映射到类似的东西

C:\Inetpub\wwwroot\wss\VirtualDirectories\80\wpresources\YourWebPart

如果管理员部署到GAC,那么它将是

http://server/_wpresources/WebPartStrongName/

映射到

C:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/wpresources/WebPartStrongName

要找出运行时需要的路径,您应该使用 WebPart.ClassResourcePath

所以修改你的代码

string scriptPath = this.ClassResourcePath + "/yourjs.js";
if(!this.Page.ClientScript.IsClientScriptBlockRegistered(scriptPath))
    this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), scriptPath,
"<script language=\"javascript\" src=\"" + scriptPath + "\">");

MSDN - 使用客户端脚本创建Web部件


2
2018-03-12 20:28





我知道这是一个老帖子,但有一个更好的方法来做到这一点。这种方式更好,因为它将js和CSS文件保存在项目的本地。我发现如果可能的话,最好不要使用12个蜂巢,并且添加CSS和js文件是可能的。

首先,您应该自己创建一个文件夹,将您的js文件和CSS文件放在C#项目的顶层。将您的js / CSS文件放入此文件夹。

接下来,您将创建一个将调用您的js或CSS文件的Constants.cs类。这是该类的代码。

using System;
using System.Collections.Generic;
using System.Text;

namespace myProjectSolution {

internal static class Constants
{
    public const string JQuerymyjavascriptFileJS = "myProjectSolution.Resources.myjavascriptFile.js";

public const string CSSPath = "myProjectSolution.Resources.CSSPath.css";
        }

}

请注意,在代码中它正在重新生成Resources文件夹。这是我在第一步中创建的额外文件夹。称之为你想要的。

下一部分是将您对js文件/ CSS的引用添加到程序集中。

[assembly: WebResource(Constants.JQuerymyjavascriptFileJS, "text/javascript")]
[assembly: WebResource(Constants.CSSPath, "text/css")]

完成此操作后,您就可以在WSP项目的主C#类中调用并使用js / CSS。

protected override void OnLoad(EventArgs e)
    {

        base.OnLoad(e);
        this.EnsureChildControls();

        Page.ClientScript.RegisterClientScriptResource(this.GetType(), Constants.JQuerymyjavascriptFileJS);
               }

CSS文件有点不同

ClientScriptManager csm = Page.ClientScript();

csm.RegisterClientScriptResourse(this.GetType(), Constants.CSSPath);

HtmlLink css = new HtmlLink();
css.Href = csm.GetWebResourceUrl(this.GetType(), Constants.CSSPath);
css.Attributes.Add("type", "text/css");
css.Attributes.Add("rel", "stylesheet");
Page.Header.Controls.Add(css);

我发现这是调用js和CSS文件的最佳和最可靠的方法。这就是我为所有Web部件加载JavaScript和CSS文件的方法。这样做我从来没有遇到过这样的问题。我希望这可以帮助一些有这个问题的人。 SharePoint永远不会让事情变得轻松:)


2
2017-07-15 14:10





我有一些问题让lazoDev的解决方案工作(拼写错误,缺少参考,以及编码错误),并认为我会分享我的解决方案。


第1步:Webpart称为“日历”

我的样式位于Styles文件夹中,如下所示: ProjectName/Styles/

脚本位于Scripts文件夹中: ProjectName/Scripts/

using System.Web.UI.HtmlControls;

namespace ProjectName.Calendar
{
    internal static class Styles
    {
        public const string main = "ProjectName.Styles.main.css";
        public const string calendar = "ProjectName.Styles.calendar.css";
    }

    internal static class Scripts
    {
        public const string jqueryMin = "ProjectName.Scripts.jquery.min.js";
        public const string jqueryDatepicker = "ProjectName.Scripts.jquery.datepicker.js";
    }

    [ToolboxItemAttribute(false)]
    public class Calendar: WebPart
    {
        protected override void CreateChildControls()
        {
            // Adding Styles (also have to add to AssemblyInfo.cs, as well as changing the Build Action property of the file to embedded resource)
            HtmlLink css = new HtmlLink();
            css.Attributes.Add("type", "text/css");
            css.Attributes.Add("rel", "stylesheet");
            css.Href = Page.ClientScript.GetWebResourceUrl(this.GetType(), Styles.dailog);
            Page.Header.Controls.Add(css);

            HtmlLink css2 = new HtmlLink();
            css2.Attributes.Add("type", "text/css");
            css2.Attributes.Add("rel", "stylesheet");
            css2.Href = Page.ClientScript.GetWebResourceUrl(this.GetType(), Styles.calendar);
            Page.Header.Controls.Add(css2);

            // Adding Scripts (also have to add to AssemblyInfo.cs, as well as changing the Build Action property of the file to embedded resource)
            Page.ClientScript.RegisterClientScriptResource(this.GetType(), Scripts.jqueryMin);
            Page.ClientScript.RegisterClientScriptResource(this.GetType(), Scripts.jqueryDatepicker);
        }
    }
}


第2步:AssemblyInfo.cs

using ProjectName.Calendar;

// Styling
[assembly: WebResource(Styles.main, "text/css")]
[assembly: WebResource(Styles.calendar, "text/css")]

// Scripts 
[assembly: WebResource(Scripts.jqueryMin, "text/javascript")]
[assembly: WebResource(Scripts.jqueryDatepicker, "text/javascript")]


第3步:文件属性

转到每个脚本和样式文件的属性并更改其值 Build Action 至 Embedded Resource


1
2017-07-24 20:37