protected void Page_Load(object sender, EventArgs e)
    {
        // Setup info/error message placeholder
        if (MessagesPlaceHolder != null)
        {
            MessagesPlaceHolder.UseRelativePlaceHolder = false;
            form.EnsureMessagesPlaceholder(MessagesPlaceHolder);
        }

        // Load default properties from XML
        XmlDocument xml = new XmlDocument();

        xml.Load(ResolveUrl(HttpContext.Current.Server.MapPath(PortalFormHelper.BEFOREPROPERTIESDEFINITION)));

        // Load UI element and element's page template (if any)
        mUIElementInfo = UIElementInfoProvider.GetUIElementInfo(QueryHelper.GetInteger("elementID", 0));

        if (mUIElementInfo != null)
        {
            PageTemplateInfo pti = null;
            if (mUIElementInfo.ElementType == UIElementTypeEnum.PageTemplate)
            {
                pti = PageTemplateInfoProvider.GetPageTemplateInfo(mUIElementInfo.ElementPageTemplateID);
            }

            form.Enabled = ((SystemContext.DevelopmentMode || mUIElementInfo.ElementIsCustom) && (!UIElementInfoProvider.AllowEditOnlyCurrentModule || (mUIElementInfo.ElementResourceID == QueryHelper.GetInteger("moduleId", 0))));

            // Create form info
            FormInfo fi = (pti != null) ? pti.PageTemplatePropertiesForm : PortalFormHelper.GetUIElementDefaultPropertiesForm(UIElementPropertiesPosition.Both);
            form.FormInformation = fi;

            // Load data row from properties
            DataRow dr = fi.GetDataRow();
            fi.LoadDefaultValues(dr);
            if ((mUIElementInfo == null) || !mUIElementInfo.IsApplication)
            {
                fi.RemoveFormField("DescriptionLink");
            }

            XmlData customData = new XmlData();

            // Load element properties
            XmlData data = new XmlData();
            data.AllowMacros = true;
            data.LoadData(mUIElementInfo.ElementProperties);

            form.MacroTable = data.MacroTable;

            // Fill template datarow with element's properties. If template does not contain such column, add it to custom
            foreach (String col in data.ColumnNames)
            {
                if (col.StartsWith(UIContextData.CATEGORYNAMEPREFIX, StringComparison.Ordinal))
                {
                    if (!RequestHelper.IsPostBack())
                    {
                        String           catName = col.Substring(UIContextData.CATEGORYNAMEPREFIX.Length);
                        FormCategoryInfo fci     = fi.GetFormCategory(catName);
                        if (fci != null)
                        {
                            fci.SetPropertyValue(FormCategoryPropertyEnum.CollapsedByDefault, ValidationHelper.GetBoolean(data[col], false).ToString().ToLowerInvariant());
                        }
                    }
                }
                else
                {
                    if (dr.Table.Columns.Contains(col))
                    {
                        DataHelper.SetDataRowValue(dr, col, data[col]);
                    }
                    else
                    {
                        customData[col] = data[col];
                    }
                }
            }

            dr["CustomProperties"] = customData.GetData();

            form.DataRow = dr;
            form.LoadData(dr);
            form.FormInformation = fi;

            // Master page style for margin
            CurrentMaster.PanelContent.CssClass = "WebpartProperties PageContent";

            form.OnAfterSave += form_OnAfterSave;
        }
        else
        {
            EditedObject = null;
        }
    }
    void EditForm_OnBeforeSave(object sender, EventArgs e)
    {
        elementInfo = (UIElementInfo)EditForm.EditedObject;

        // Clear icon field based on icon type
        int type = EditForm.FieldControls["ElementIconType"].Value.ToInteger(0);

        if (type == 0)
        {
            elementInfo.ElementIconClass = "";
        }
        else
        {
            elementInfo.ElementIconPath = "";
        }

        // Check unique code name
        UIElementInfo testUI = UIElementInfoProvider.GetUIElementInfo(elementInfo.ElementResourceID, elementInfo.ElementName);

        if ((testUI != null) && (testUI.ElementID != elementInfo.ElementID))
        {
            ShowError(GetString("ui.element.alreadyexists"));
            EditForm.StopProcessing = true;
            return;
        }

        UIElementInfo oldItem = UIContext.EditedObject as UIElementInfo;

        // If new element or display name has changed or parent changed, refresh tree and recalculate order
        if ((oldItem != null) && ((oldItem.ElementParentID != elementInfo.ElementParentID) || (oldItem.ElementDisplayName != elementInfo.ElementDisplayName)) || (ElementID == 0))
        {
            // If element is new or changed parent, put him in the end of the order, otherwise it stays on it's place
            if ((ElementID == 0) || (oldItem.ElementParentID != elementInfo.ElementParentID))
            {
                elementInfo.ElementOrder = UIElementInfoProvider.GetLastElementOrder(elementInfo.ElementParentID) + 1;
            }
            refreshTree = true;
        }

        // Clear target URL for page template type
        if (elementInfo.ElementType == UIElementTypeEnum.PageTemplate)
        {
            elementInfo.ElementTargetURL = null;
        }
        else
        {
            // Empty Page Template ID for non page template type
            elementInfo.ElementPageTemplateID = 0;
        }

        // Add javascript prefix for TargetURL if javascript type is selected
        if ((elementInfo.ElementType == UIElementTypeEnum.Javascript) && !elementInfo.ElementTargetURL.StartsWithCSafe("javascript", true))
        {
            elementInfo.ElementTargetURL = "javascript:" + elementInfo.ElementTargetURL;
        }

        if (isNew)
        {
            elementInfo.ElementIsCustom = !SystemContext.DevelopmentMode;
        }

        // For new elements or when template is changed, create new element's properties based on default values
        bool templateChanged = ValidationHelper.GetBoolean(EditForm.FieldControls["ElementPageTemplateID"].GetValue("TemplateChanged"), false);

        if (isNew || templateChanged)
        {
            // Get page template if any template is selected
            PageTemplateInfo pti = (elementInfo.ElementPageTemplateID > 0) ? PageTemplateInfoProvider.GetPageTemplateInfo(elementInfo.ElementPageTemplateID) : null;

            // Create form info based on either template combined with default general data (XML file) or default general data only (if no template is selected)
            FormInfo fi = (pti != null) ? pti.PageTemplatePropertiesForm : PortalFormHelper.GetUIElementDefaultPropertiesForm(UIElementPropertiesPosition.Both);

            // Create XMLData collection for current element (this make sense only for template change).
            XmlData data = new XmlData();
            data.LoadData(elementInfo.ElementProperties);

            // Apply default data to element's properties, but only if has not it's own data (based by column name)
            foreach (FormFieldInfo ffi in fi.GetFields(true, true))
            {
                if (!data.ColumnNames.Contains(ffi.Name) && !String.IsNullOrEmpty(ffi.DefaultValue))
                {
                    data[ffi.Name] = ffi.DefaultValue;
                }
            }

            elementInfo.ElementProperties = data.GetData();
        }
    }