Add CSS and JS references in Sandbox
Â
These helper methods use javascript to dynamically load js and css resources and work within Sandbox Web Parts.
Â
public static void SandboxRegisterClientScriptBlock(string id, string url, Control control) { HtmlGenericControl script = new HtmlGenericControl("script"); script.Attributes.Add("type", "text/javascript"); script.InnerHtml = @"function wpLoad" + id + @"() { if(document.getElementById('" + id + @"') == null) { var script = document.createElement('script'); script.type = 'text/javascript'; script.id = '" + id + @"'; script.src = '" + url + @"'; document.body.appendChild(script); } } _spBodyOnLoadFunctionNames.push('wpLoad" + id + @"');"; control.Controls.Add(script); } public static void SandboxRegisterCss(string id, string url, Control control) { HtmlGenericControl css = new HtmlGenericControl("script"); css.Attributes.Add("type", "text/javascript"); css.InnerHtml = @"function wpLoad" + id + @"() { if(document.getElementById('" + id + @"') == null) { var script = document.createElement('link'); script.type = 'text/css'; script.rel = 'stylesheet'; script.id = '" + id + @"'; script.href = '" + url + @"'; document.body.appendChild(script); } } _spBodyOnLoadFunctionNames.push('wpLoad" + id + @"');"; control.Controls.Add(css); }
CodeMonkey Software is a division of JCHMedia www.jchmedia.com