/// <summary>
        /// Render out the correct markup for the tree
        /// </summary>
        /// <remarks>
        /// Since we're inheriting from a UserControl, we need to render out the markup manually
        /// </remarks>
        /// <param name="writer"></param>
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            //You'll notice that we're replacing the 'serviceUrl' parameter with our own custom web service!

            writer.Write(@"
<script type=""text/javascript"">
jQuery(document).ready(function() {
    var ctxMenu = " + GetJSONContextMenu() + @";	
    var app = """ + App + @""";
    var showContext = " + ShowContextMenu.ToString().ToLower() + @";
    var isDialog = " + IsDialog.ToString().ToLower() + @";
    var dialogMode = """ + DialogMode.ToString() + @""";
    var treeType = """ + TreeType + @""";
    var functionToCall = """ + FunctionToCall + @""";
    var nodeKey = """ + NodeKey + @""";
	
    //create the javascript tree
    jQuery(""#" + ClientID + @""").UmbracoTree({
        doNotInit: " + ManualInitialization.ToString().ToLower() + @",
        jsonFullMenu: ctxMenu,
        appActions: UmbClientMgr.appActions(),
        deletingText: '" + umbraco.ui.GetText("deleting") + @"',
        app: app,
        showContext: showContext,
        isDialog: isDialog,
        dialogMode: dialogMode,
        treeType: treeType,
        functionToCall : functionToCall,
        nodeKey : nodeKey,
        treeMode: """ + Mode.ToString().ToLower() + @""",
        dataUrl: """ + IOHelper.ResolveUrl(SystemDirectories.Umbraco) + @"/webservices/TreeDataService.ashx"",
        serviceUrl: """ + IOHelper.ResolveUrl(SystemDirectories.Umbraco) + @"/controls/Tree/CustomTreeService.asmx/GetInitAppTreeData""});
        
     //add event handler for ajax errors, this will refresh the whole application
    var mainTree = UmbClientMgr.mainTree();
    if (mainTree != null) {
        mainTree.addEventHandler(""ajaxError"", function(e) {
            if (e.msg == ""rebuildTree"") {
	            UmbClientMgr.mainWindow(""umbraco.aspx"");
            }
        });
    }
});	

</script>");

            //render the controls
            TreeContainer.RenderControl(writer);
        }
        /// <summary>
        /// Return a serialized string of values for the pre value editor model
        /// </summary>
        /// <returns></returns>
        public override string GetSerializedValue()
        {
            var xml = new XElement("preValues",
                                   new XElement("preValue", new XAttribute("name", "ShowLabel"), new XCData(ShowLabel.ToString())),
                                   new XElement("preValue", new XAttribute("name", "ShowContextMenu"), new XCData(ShowContextMenu.ToString())),
                                   new XElement("preValue", new XAttribute("name", "Size"),
                                                new XCData(Size.Width + "x" + Size.Height)),
                                   new XElement("preValue", new XAttribute("name", "Features"),
                                                new XCData(
                                                    string.Join(",", Features
                                                                .Where(x => x.Selected)
                                                                .Select(x => x.Value).ToArray()))),
                                   new XElement("preValue", new XAttribute("name", "Stylesheets"),
                                                new XCData(
                                                    string.Join(",", Stylesheets
                                                                .Where(x => x.Selected)
                                                                .Select(x => x.Value).ToArray()))),
                                   new XElement("preValue", new XAttribute("name", "IsRequired"),
                                                new XCData(IsRequired.ToString())));

            if (!ValidElements.IsNullOrWhiteSpace())
            {
                xml.Add(new XElement("preValue", new XAttribute("name", "validElements"), new XCData(ValidElements.Trim())));
            }

            return(xml.ToString());
        }