Try Now

JS Include User Control

by Mustafa Kipergil
Publication date:
Nov 27, 2009, 10:16 AM


  • Sitefinity 3.7
  • Sitefinity 3.6

Price: $ 0

For some pages you need to include some custom js files that are only used in that page. When this is the case including those js files to a master page is quite unnecessary. It loads it in every page.

So by this control you can easily include your custom javascript files just by dragging it onto a page.

You only need to fill the js file path property like: ~/customJsFiles/scripts.js

Example Code:
using System;  
using System.Collections;  
using System.Configuration;  
using System.Data;  
using System.Linq;  
using System.Web;  
using System.Web.Security;  
using System.Web.UI;  
using System.Web.UI.HtmlControls;  
using System.Web.UI.WebControls;  
using System.Web.UI.WebControls.WebParts;  
using System.Xml.Linq;  
using System.ComponentModel;  
public partial class JSInclude : System.Web.UI.UserControl  
    [Bindable(true), Category("Settings"), Description("This is the url of the script. Use (~) to reference the file. Ex : ~/js/jquery.plugin.js"), DefaultValue("")]  
    private string jsUrl;  
    public string JSUrl  
            return this.jsUrl;  
            this.jsUrl = value;  
    protected void Page_Load(object sender, EventArgs e)  
        if (this.Page != null && this.Page.Header != null)  
            string url = Page.ResolveUrl(this.JSUrl);  
            HtmlGenericControl jsLink = new HtmlGenericControl("script");  
            jsLink.Attributes["type"] = "text/javascript";  
            jsLink.Attributes["src"] = url;  
            if (!string.IsNullOrEmpty(url))