Sharepoint Web 部件中的外部 Javascript 文件

发布于 2024-10-20 21:13:28 字数 1052 浏览 2 评论 0原文

我正在创建一个共享点 Web 部件,我想在其中调用外部 javascript 文件。我在以下位置创建了 .js 文件

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\CustomJScripts

调用函数时,其给出的函数未找到错误。 javascript文件的位置是否错误? 以下是代码:

protected override void CreateChildControls()
{
    Page.ClientScript.RegisterStartupScript(
        this.GetType(), 
        this.ID, 
        "_spOriginalFormAction = document.forms[0].action;", 
        true);


       ClientScriptManager cs = Page.ClientScript;
        if (!cs.IsClientScriptIncludeRegistered("OnMouseOverScript"))
        cs.RegisterClientScriptInclude(
            this.GetType(), 
            "OnMouseOverScript", 
            ResolveUrl("/_layouts/CustomJScripts/MyJS.js"));
}

private void GetData(string strSchCode)
{

     Table t = new Table();

     TableRow tr = new TableRow();
     TableCell tc = new TableCell();

     tc.Attributes.Add("onmouseover", "return ShowInfo('AA');");
     tr.Controls.Add(tc);
     t.Controls.Add(tr);
     this.Controls.Add(t);
}

I am creating a sharepoint web part in which i want to call external javascript file. I have created .js file in following location

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\CustomJScripts

Its giving function not found error when function is called. Is the location of javascript file wrong?
Following is the code :

protected override void CreateChildControls()
{
    Page.ClientScript.RegisterStartupScript(
        this.GetType(), 
        this.ID, 
        "_spOriginalFormAction = document.forms[0].action;", 
        true);


       ClientScriptManager cs = Page.ClientScript;
        if (!cs.IsClientScriptIncludeRegistered("OnMouseOverScript"))
        cs.RegisterClientScriptInclude(
            this.GetType(), 
            "OnMouseOverScript", 
            ResolveUrl("/_layouts/CustomJScripts/MyJS.js"));
}

private void GetData(string strSchCode)
{

     Table t = new Table();

     TableRow tr = new TableRow();
     TableCell tc = new TableCell();

     tc.Attributes.Add("onmouseover", "return ShowInfo('AA');");
     tr.Controls.Add(tc);
     t.Controls.Add(tr);
     this.Controls.Add(t);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

这样的小城市 2024-10-27 21:13:28

您必须将此 javascript 添加到您的 Web 部件。在我的网络部件中,我使用这种方法:

    protected override void OnPreRender(EventArgs e)
    {
        Page.ClientScript.RegisterStartupScript(GetType(), "MyScript",
            "<SCRIPT language='javascript' src='~/_layouts/CustomJScripts/MyJS.js'></SCRIPT>", false);
        base.OnPreRender(e);
    }

You have to add this javascript to your webpart. In my webpart I'm using this method:

    protected override void OnPreRender(EventArgs e)
    {
        Page.ClientScript.RegisterStartupScript(GetType(), "MyScript",
            "<SCRIPT language='javascript' src='~/_layouts/CustomJScripts/MyJS.js'></SCRIPT>", false);
        base.OnPreRender(e);
    }
你列表最软的妹 2024-10-27 21:13:28

也许单引号有问题?例如使用双引号而不是单引号:

tc.Attributes.Add("onmouseover", "return ShowInfo(\"AA\");");

Maybe there's an issue with the single quotes? e.g. use double quotes instead of single:

tc.Attributes.Add("onmouseover", "return ShowInfo(\"AA\");");
予囚 2024-10-27 21:13:28

我会使用 ScriptLink.Register 方法,然后将文件移至 14 \模板\布局\1033\CustomJScripts。

ScriptLink 封装了 ClientScriptManager 调用以及附加功能。 name 参数是相对路径,这就是为什么 javascript 文件需要位于 14\TEMPLATE\LAYOUTS\ LCID 目录中(其中 LCID 是您的语言编号)。

你的代码看起来像这样:

protected override void CreateChildControls()
{
    Page.ClientScript.RegisterStartupScript(
        this.GetType(), 
        this.ID, 
        "_spOriginalFormAction = document.forms[0].action;", 
        true);

    ScriptLink.Register(this.Page, "CustomJScripts/MyJS.js", true);
}

I would use the ScriptLink.Register method and then move your file to 14\TEMPLATE\LAYOUTS\1033\CustomJScripts.

ScriptLink encapsulates the ClientScriptManager calls along with additional functionality. The name parameter is a relative path, which is why the javascript file needs to be in the 14\TEMPLATE\LAYOUTS\ LCID directory (where LCID is your language number).

Your code would look something like this:

protected override void CreateChildControls()
{
    Page.ClientScript.RegisterStartupScript(
        this.GetType(), 
        this.ID, 
        "_spOriginalFormAction = document.forms[0].action;", 
        true);

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