예제 #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);
                        }
                    }
                }
            }
        }