private void btnDeleteDetail_Click(object sender, EventArgs e)
        {
            KPPageBase pageBase = this.Page as KPPageBase;
            if (this.HiddenKey != null && !String.IsNullOrEmpty(HiddenKey.Value) && !String.IsNullOrEmpty(PageFormUrl) && pageBase != null)
            {
                object entitySelected = GetSelectedEntityDetail();

                if (MasterDetailConfig.KPEventBeforeDeleteDetailDelegate != null)
                {
                    KPButtonEventsArgs eArgs = new KPButtonEventsArgs();
                    MasterDetailConfig.KPEventBeforeDeleteDetailDelegate(entitySelected, eArgs);
                    if (eArgs.Cancel)
                    {
                        pageBase.SetLoading(false);
                        return;
                    }
                }

                DetailSession detailSession = KPSessionHelper.GetSessionMasterDetailList(pageBase.SessionPageID, this.MasterDetailConfig.MasterDetailID);
                int valueKey = 0;
                if (Int32.TryParse(HiddenKey.Value, out valueKey))
                {
                    if (valueKey > 0)
                        detailSession.DetailsEntity.Add(new DetailEntity(entitySelected as ActiveRecordBase, StateDetailEntity.Deleted));
                }
                detailSession.Entities.Remove(entitySelected);
                DataBind();

                if (MasterDetailConfig.KPEventAfterDeleteDetailDelegate != null)
                {
                    MasterDetailConfig.KPEventAfterDeleteDetailDelegate(entitySelected);
                }
            }

            pageBase.SetLoading(false);
        }
        private void btnNewDetail_Click(object sender, EventArgs e)
        {
            KPPageBase pageBase = this.Page as KPPageBase;
            if (!String.IsNullOrEmpty(PageFormUrl) && pageBase != null)
            {
                ActiveRecordBase entityDetail = Activator.CreateInstance(TypeEntity) as ActiveRecordBase;
                if (entityDetail != null &&
                        this.NamingContainer != null &&
                            this.NamingContainer is KPFormControl)
                {
                    KPFormBaseControl formControl = this.NamingContainer as KPFormControl;
                    entityDetail = SetDetailsKeys(formControl.DataSourceAltered as ActiveRecordBase, entityDetail, MasterDetailConfig.KeyFieldsConfig);
                }

                if (MasterDetailConfig.KPEventBeforeNewDetailDelegate != null)
                {
                    KPButtonEventsArgs eArgs = new KPButtonEventsArgs();
                    MasterDetailConfig.KPEventBeforeNewDetailDelegate(entityDetail, eArgs);
                    if (eArgs.Cancel)
                    {
                        pageBase.SetLoading(false);
                        return;
                    }
                }

                KPSessionHelper.SetSessionData(new KPSessionDetailData(this, entityDetail, KPFormStateEnum.New, this.MasterDetailConfig.MasterDetailID), pageBase.SessionPageID);
                string titlePage = "Carregando ...";
                string pageFormUrlSession = String.Format("{0}?parentID={1}", this.PageFormUrl, pageBase.SessionPageID);
                pageBase.KPWindow(titlePage, pageFormUrlSession, true, WidthForm, HeightForm);
            }
        }
        private void btnEditDetail_Click(object sender, EventArgs e)
        {
            KPPageBase pageBase = this.Page as KPPageBase;
            if (this.HiddenKey != null && !String.IsNullOrEmpty(HiddenKey.Value) && !String.IsNullOrEmpty(PageFormUrl) && pageBase != null)
            {
                object entitySelected = GetSelectedEntityDetail();

                if (MasterDetailConfig.KPEventBeforeEditDetailDelegate != null)
                {
                    KPButtonEventsArgs eArgs = new KPButtonEventsArgs();
                    MasterDetailConfig.KPEventBeforeEditDetailDelegate(entitySelected, eArgs);
                    if (eArgs.Cancel)
                    {
                        pageBase.SetLoading(false);
                        return;
                    }
                }

                KPSessionHelper.SetSessionData(new KPSessionDetailData(this, entitySelected, KPFormStateEnum.Edit, this.MasterDetailConfig.MasterDetailID), pageBase.SessionPageID);
                string titlePage = "Carregando ...";
                string pageFormUrlSession = String.Format("{0}?parentID={1}", this.PageFormUrl, pageBase.SessionPageID);
                pageBase.KPWindow(titlePage, pageFormUrlSession, true, WidthForm, HeightForm);
            }
        }
        public bool SaveEntity()
        {
            try
            {
                // Reset DataSource
                DataSourceAltered = CloneDataSource(DataSource, TypeEntity);

                //Pega os dados da tela e preenche o DataSource
                RefreshDataSourceAltered();

                if (DataSourceAltered != null)
                {
                    if (Validate(ValidateEntity()))
                    {
                        if (KPEventBeforeSaveClick != null)
                        {
                            KPButtonEventsArgs eventArgs = new KPButtonEventsArgs();
                            KPEventBeforeSaveClick(DataSourceAltered, eventArgs);
                            if (eventArgs.Cancel)
                                return false;
                        }

                        ConstructorInfo entityBO = TypBOEntity.GetConstructor(new[] { TypeEntity });
                        if (entityBO == null)
                        {
                            throw new Exception(String.Format("Could not find a valid Constructor to this Entity ({0})", TypeEntity.FullName));
                        }
                        object entityBOInstance = entityBO.Invoke(new object[] { DataSourceAltered });

                        MethodInfo methodValidate = TypBOEntity.GetMethod("Validate");
                        if (methodValidate == null)
                        {
                            throw new Exception(String.Format("Could not find a valid 'Validate' method to this EntityBO ({0})", TypBOEntity.FullName));
                        }
                        object validateReturn = methodValidate.Invoke(entityBOInstance, null);

                        if (validateReturn != null && !((bool)validateReturn))
                        {
                            PropertyInfo propInvalidValues = TypBOEntity.GetProperty("InvalidValues");
                            List<InvalidValue> invalidValues = propInvalidValues.GetValue(entityBOInstance, null) as List<InvalidValue>;
                            if (invalidValues.Count > 0)
                            {
                                if (!Validate(invalidValues.ToArray()))
                                    return false;
                            }

                            PropertyInfo propInvalidEntity = TypBOEntity.GetProperty("InvalidEntityHeader");
                            List<InvalidValueBO> invalidEntityValues = propInvalidEntity.GetValue(entityBOInstance, null) as List<InvalidValueBO>;
                            if (invalidEntityValues.Count > 0)
                            {
                                this.ErrorsView.Visible = true;
                                StringBuilder erroMsg = new StringBuilder();
                                foreach (InvalidValueBO invalidValueBO in invalidEntityValues)
                                {
                                    erroMsg.AppendFormat(" - {0}<br>", invalidValueBO.Error);
                                }
                                this.ErrorsView.InnerHtml = erroMsg.ToString();
                                return false;
                            }
                        }

                        if (KPEventSaveClick != null)
                        {
                            KPEventSaveClick(entityBOInstance);
                        }
                        else
                        {
                            MethodInfo methodSave = TypBOEntity.GetMethod("SaveEntityBase");
                            object entitySaved = methodSave.Invoke(entityBOInstance, null);
                            DataSource = entitySaved;
                            AfterSaveEntity(DataSource as ActiveRecordBase);

                            if (KPEventAfterSaveClick != null)
                            {
                                KPEventAfterSaveClick(entityBOInstance);
                            }
                        }

                        var eventFormArgs = new KPFormEventArgs();
                        eventFormArgs.Entity = this.DataSourceAltered;
                        if (KPEventFormClosing != null)
                            KPEventFormClosing(this, eventFormArgs);

                        ClosePage();
                    }
                    else
                    {
                        return false;
                    }
                }

                return true;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null && ex.InnerException is KPExceptionValidator)
                {
                    KPExceptionValidator exception = ex.InnerException as KPExceptionValidator;
                    this.ErrorsView.Visible = true;
                    StringBuilder erroMsg = new StringBuilder();
                    foreach (InvalidValue error in exception.Erros)
                    {
                        erroMsg.AppendFormat(" - {0} <br>", error.Message);
                    }
                    this.ErrorsView.InnerHtml = erroMsg.ToString();

                    return false;
                }

                throw ex;
            }
        }