Пример #1
0
        /// <summary>
        /// Writes the data in the given webform to the given entity object
        /// </summary>
        /// <param name="form"></param>
        /// <param name="entity"></param>
        public static void FormToEntity(System.Web.UI.Control form, object entity)
        {
            PropertyInfo[] properties = entity.GetType().GetProperties();

            //loop the properties of the entity, try to find a matching webcontrol for each one
            foreach (var property in properties)
            {
                var control = WebControlManager.GetControl(property.Name, form);

                if (control != null)
                {
                    var value = WebControlManager.GetValue(control);

                    //convert to correct type...
                    if (property.CanWrite)
                    {
                        var convertedValue = ConvertValue(value, property.PropertyType);

                        //only set value if conversion was successful
                        if (convertedValue.GetType().Equals(property.PropertyType))
                        {
                            property.SetValue(entity, ConvertValue(value, property.PropertyType), null);
                        }
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Writes the data in the given entity object to the given form
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="form"></param>
        public static void EntityToForm(object entity, System.Web.UI.Control form)
        {
            PropertyInfo[] properties = entity.GetType().GetProperties();

            //loop the properties of the entity, try to find a matching webcontrol for each one
            foreach (var property in properties)
            {
                var control = WebControlManager.GetControl(property.Name, form);

                if (control != null)
                {
                    var value = property.GetValue(entity, null);
                    if (value != null)
                    {
                        WebControlManager.SetValue(control, value.ToString());
                    }
                }
            }
        }