Exemplo n.º 1
0
        public void ReverseBind()
        {
            using (LogGroup logGroup = LogGroup.Start("Transferring data from form fields to entity.", NLog.LogLevel.Debug))
            {
                if (DataSource == null)
                {
                    LogWriter.Debug("DataSource == null - Reverse bind skipped");
                }
                else
                {
                    ActivateStrategy.New((IEntity)DataSource).Activate((IEntity)DataSource);

                    foreach (TableRow row in this.Rows)
                    {
                        if (row is EntityFormItem)
                        {
                            using (LogGroup logGroup2 = LogGroup.StartDebug("Checking row with field control ID '" + ((EntityFormItem)row).FieldControlID + "'."))
                            {
                                LogWriter.Debug("Enabled: " + row.Enabled);
                                LogWriter.Debug("Visible: " + row.Visible);

                                if (row.Enabled && row.Visible)
                                {
                                    LogWriter.Debug("Enabled and visible.");

                                    EntityFormItem item = (EntityFormItem)row;

                                    if (item.AutoBind && item.PropertyName != null && item.PropertyName != String.Empty)
                                    {
                                        LogWriter.Debug("Property name: " + item.PropertyName);

                                        WebControl field = (WebControl)FindControl(item.FieldControlID);
                                        // Skip label fields, they're not editable
                                        if (field.GetType() != typeof(Label) &&
                                            field.Enabled)
                                        {
                                            Type propertyType = Reflector.GetPropertyType(DataSource, item.PropertyName);

                                            LogWriter.Debug("Property type: " + (propertyType == null ? "[null]" : propertyType.ToString()));

                                            object value = EntityFormHelper.GetFieldValue(field, item.ControlValuePropertyName, propertyType);

                                            LogWriter.Debug("Field value type: " + (value == null ? "[null]" : value.GetType().ToString()));
                                            LogWriter.Debug("Field value: " + (value == null ? "[null]" : value.ToString()));

                                            object castValue = EntityFormHelper.Convert(value, propertyType);

                                            Reflector.SetPropertyValue(DataSource, item.PropertyName, castValue);
                                        }
                                    }
                                    else
                                    {
                                        LogWriter.Debug("Not auto binding or no property name specified.");
                                    }
                                }
                                else
                                {
                                    LogWriter.Debug("Skipping.");
                                }
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        public override void DataBind()
        {
            using (LogGroup logGroup = LogGroup.Start("Binding the DataSource data to the values of the fields in the EntityForm.", NLog.LogLevel.Debug))
            {
                base.DataBind();

                foreach (TableRow row in Rows)
                {
                    if (DataSource != null)
                    {
                        LogWriter.Debug("DataSource != null");

                        if (row is EntityFormItem)
                        {
                            LogWriter.Debug("row is EntityFormItem");

                            EntityFormItem item         = (EntityFormItem)row;
                            string         propertyName = ((EntityFormItem)item).PropertyName;

                            if (item.AutoBind && propertyName != String.Empty)
                            {
                                using (LogGroup logGroup2 = LogGroup.Start("Property: " + propertyName, NLog.LogLevel.Debug))
                                {
                                    LogWriter.Debug("Field control ID: " + item.FieldControlID);

                                    object propertyValue = Reflector.GetPropertyValue(DataSource, propertyName);
                                    Type   propertyType  = Reflector.GetPropertyType(DataSource, propertyName);

                                    LogWriter.Debug("Property type: " + propertyType.ToString());

                                    if (propertyValue == null)
                                    {
                                        LogWriter.Debug("PropertyValue: [null]");
                                    }
                                    else
                                    {
                                        LogWriter.Debug("Property value: " + propertyValue.ToString());

                                        if (propertyValue is Array)
                                        {
                                            LogWriter.Debug("Property array length: " + ((Array)propertyValue).Length.ToString());
                                        }
                                    }

                                    Control field = FindControl(item.FieldControlID);

                                    EntityFormHelper.SetFieldValue(field, propertyValue, item.ControlValuePropertyName, propertyType);
                                }
                            }
                        }
                        else
                        {
                            LogWriter.Debug("!(row is EntityFormItem)");
                        }
                    }
                    else
                    {
                        LogWriter.Debug("DataSource == null");
                    }
                }
            }
        }