private bool ApplyChanges()
        {
            using (SPLimitedWebPartManager wpm = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
            {

                foreach (System.Web.UI.WebControls.WebParts.WebPart wpm_webpart in wpm.WebParts)
                {
                    if (this.Page.Request.QueryString[WEBPARTIDQSKEYNAME] == wpm_webpart.ID)
                    {
                        webpart = wpm_webpart as WebSitePart;
                        break;
                    }
                }

                if (webpart != null)
                {
                    wpm.Web.AllowUnsafeUpdates = true;


                    PropertyInfo[] properties = webpart.GetType().GetProperties();

                    foreach (PropertyInfo propertyInfo in properties)
                    {

                        Type propertyType = propertyInfo.PropertyType;
                        IDisplayableClass displayClass = WebPartServiceLocator.Current.DisplayableClasses.FirstOrDefault(c => c.IsAppliable(propertyType));
                        Control control = FindControlRecursive(section,"control_" + propertyInfo.Name.ToString());
                        if (control != null)
                        {
                            propertyInfo.SetValue(webpart, displayClass.GetControlValue(control), null);
                        }
                    }

                    wpm.SaveChanges(webpart);
                    wpm.Web.AllowUnsafeUpdates = false;

                    foreach (Microsoft.SharePoint.WebPartPages.WebPart wp in wpm.WebParts)
                    {
                        wpm.SaveChanges(wp);
                    }
                }
            }
            return true;
        }
        public override void SyncChanges()
        {
            WebPart webPart = this.WebPartToEdit;

            int i = 0;

            foreach (string propertyName in PropertyNames)
            {
                PropertyInfo      propertyInfo  = webPart.GetType().GetProperty(propertyName);
                Type              propertyType  = propertyInfo.PropertyType;
                object            propertyValue = propertyInfo.GetValue(webPart, null);
                IDisplayableClass displayClass  = WebPartServiceLocator.Current.DisplayableClasses.FirstOrDefault(c => c.IsAppliable(propertyType));

                Control control = (Control)this.FindControl("control" + i.ToString());
                displayClass.SetControlValue(control, propertyValue);

                i++;
            }
        }
        public override bool ApplyChanges()
        {
            WebPart webPart = this.WebPartToEdit;

            int i = 0;

            foreach (string propertyName in this.PropertyNames)
            {
                PropertyInfo      propertyInfo = webPart.GetType().GetProperty(propertyName);
                Type              propertyType = propertyInfo.PropertyType;
                IDisplayableClass displayClass = WebPartServiceLocator.Current.DisplayableClasses.FirstOrDefault(c => c.IsAppliable(propertyType));

                Control control = this.FindControl("control" + i.ToString());
                propertyInfo.SetValue(webPart, displayClass.GetControlValue(control), null);

                i++;
            }

            return(true);
        }
        protected override void CreateChildControls()
        {
            if (webpart != null)
            {
                PropertyInfo[] properties = webpart.GetType().GetProperties();

                foreach (PropertyInfo propertyInfo in properties)
                {
                    Type propertyType = propertyInfo.PropertyType;
                    object[] attributes = propertyInfo.GetCustomAttributes(false);

                    IDisplayableClass displayClass = WebPartServiceLocator.Current.DisplayableClasses.FirstOrDefault(c => c.IsAppliable(propertyType));

                    if (displayClass != null)
                    {

                        CategoryAttribute categoryAttribute = (CategoryAttribute)propertyInfo.GetCustomAttributes(typeof(CategoryAttribute), false).FirstOrDefault();

                        if (categoryAttribute != null)
                        {
                            HtmlGenericControl fieldset = null;
                            //HtmlGenericControl ol = null;

                            Control previous = section.FindControl("fieldset_" + categoryAttribute.Category);
                            if (previous == null)
                            {

                                fieldset = new HtmlGenericControl("fieldset");
                                fieldset.ID = "fieldset_" + categoryAttribute.Category;
                                HtmlGenericControl legend = new HtmlGenericControl("legend");
                                legend.ID = "legend_" + categoryAttribute.Category;
                                legend.InnerText = categoryAttribute.Category;
                                fieldset.Controls.Add(legend);

                                //ol = new HtmlGenericControl("ol");
                                //ol.ID = "ol_" + categoryAttribute.Category;
                                //fieldset.Controls.Add(ol);

                                section.Controls.Add(fieldset);
                            }
                            else
                            {
                                fieldset = (HtmlGenericControl)previous;
                                //ol = (HtmlGenericControl)fieldset.FindControl("ol_" + categoryAttribute.Category);
                            }


                            //HtmlGenericControl li = new HtmlGenericControl("li");
                            //li.ID = "li_" + propertyInfo.Name.ToString();
                            //ol.Controls.Add(li);

                            DisplayNameAttribute displayNameAttribute = attributes.OfType<DisplayNameAttribute>().FirstOrDefault();

                            if (displayNameAttribute != null)
                            {
                                if (displayClass.GetType() != typeof(DisplayableBoolean))
                                {
                                    Label label = new Label();
                                    label.Attributes.Add("for", displayNameAttribute.DisplayName);
                                    DescriptionAttribute descriptionAttribute = attributes.OfType<DescriptionAttribute>().FirstOrDefault();
                                    if (descriptionAttribute != null)
                                    {
                                        label.Attributes.Add("title", descriptionAttribute.Description);
                                    }

                                    label.Text = displayNameAttribute.DisplayName;
                                    //li.Controls.Add(label);
                                    fieldset.Controls.Add(label);
                                }

                                Control control = displayClass.CreateControl();

                                if (displayClass.GetType() == typeof(DisplayableBoolean))
                                {
                                    WebControl checkbox = (WebControl)control;
                                    checkbox.Attributes.Add("Text", displayNameAttribute.DisplayName);
                                    checkbox.Attributes.Add("TextAlign", "Right");
                                    fieldset.Controls.Add(checkbox);
                                }
                                else
                                {
                                    control.ID = "control_" + propertyInfo.Name.ToString();
                                    fieldset.Controls.Add(control);
                                }
                            }
                            else
                            {
                                section.Controls.Remove(fieldset);
                            }
                        }
                    }
                }
            }

            base.CreateChildControls();
        }
        private void GetValues()
        {
            try
            {
                if (webpart != null)
                {
                    PropertyInfo[] properties = webpart.GetType().GetProperties();

                    foreach (PropertyInfo propertyInfo in properties)
                    {
                        Type propertyType = propertyInfo.PropertyType;
                        object[] attributes = propertyInfo.GetCustomAttributes(false);
                        IDisplayableClass displayClass = WebPartServiceLocator.Current.DisplayableClasses.FirstOrDefault(c => c.IsAppliable(propertyType));

                        if (displayClass != null)
                        {
                            try
                            {
                                Control control = this.FindControl("control_" + propertyInfo.Name.ToString());

                                if (control != null)
                                {
                                    object propertyValue = this.ViewState[control.ID];

                                    if (propertyInfo != null || string.IsNullOrEmpty(propertyValue.ToString()))
                                    {
                                        displayClass.SetControlValue(control, propertyValue);
                                        //this.ViewState.Add(control.ID, propertyValue);
                                    }
                                    else
                                    {
                                        DefaultValueAttribute defaultValueAttribute = attributes.OfType<DefaultValueAttribute>().FirstOrDefault();
                                        if (defaultValueAttribute != null)
                                        {
                                            displayClass.SetControlValue(control, defaultValueAttribute.Value);
                                            //this.ViewState.Add(control.ID, propertyValue);
                                        }
                                        else
                                        {
                                            Control li = this.FindControl("li_" + propertyInfo.Name.ToString());
                                            if (li != null)
                                            {
                                                li.Parent.Controls.Remove(li);
                                            }
                                        }
                                    }
                                }
                            }
                            catch (Exception)
                            {
                                Control li = this.FindControl("li_" + propertyInfo.Name.ToString());
                                if (li != null)
                                {
                                    li.Parent.Controls.Remove(li);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //ol.Controls.Remove(li);
                ex.ToString();
            }
        }
        protected override void CreateChildControls()
        {
            WebPart webPart = this.WebPartToEdit;

            int i = 0;


            foreach (string propertyName in PropertyNames)
            {
                PropertyInfo propertyInfo = webPart.GetType().GetProperty(propertyName);
                Type         propertyType = propertyInfo.PropertyType;
                object[]     attributes   = propertyInfo.GetCustomAttributes(false);

                IDisplayableClass displayClass = WebPartServiceLocator.Current.DisplayableClasses.FirstOrDefault(c => c.IsAppliable(propertyType));

                Control control = displayClass.CreateControl();
                control.ID = "control" + i.ToString();

                Panel sectionHead = new Panel();
                sectionHead.CssClass = "UserSectionHead";

                WebDisplayNameAttribute displayNameAttribute = attributes.OfType <WebDisplayNameAttribute>().FirstOrDefault();
                if (displayNameAttribute != null)
                {
                    if (displayClass.IsControlInHeaderSection())
                    {
                        sectionHead.Controls.Add(control);
                        sectionHead.Controls.Add(new LiteralControl(" "));
                    }
                    sectionHead.Controls.Add(new LiteralControl(displayNameAttribute.DisplayName));
                }

                this.Controls.Add(sectionHead);

                if (!displayClass.IsControlInHeaderSection())
                {
                    Panel sectionBody = new Panel();
                    sectionBody.CssClass = "UserSectionBody";
                    sectionBody.Style.Add("padding-bottom", "8px");

                    sectionBody.Controls.Add(control);

                    this.Controls.Add(sectionBody);
                }

                if (i < PropertyNames.Length - 1)
                {
                    Panel sectionDottedLine = new Panel();
                    sectionDottedLine.CssClass = "UserDottedLine";
                    sectionDottedLine.Style.Add("width", "100%");
                    sectionDottedLine.Style.Add("margin-bottom", "5px");
                    this.Controls.Add(sectionDottedLine);
                }

                i++;
            }


            SyncChanges();

            base.CreateChildControls();
        }