开发者

External Javascript File in Sharepoint web part

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 Show开发者_如何学CInfo('AA');");
     tr.Controls.Add(tc);
     t.Controls.Add(tr);
     this.Controls.Add(t);
}


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);
    }


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

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


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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜