1 namespace System.Web.DynamicData { 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 using System.Web.UI.WebControls; 7 using System.Web.Hosting; 8 9 // This FieldTemplateFactory is used for the simple cases where the user doesn't have 10 // a FieldTemplate directory but wants to get basic validation logic. In a sense 11 // it is a smarter version of a BoundField 12 internal class SimpleFieldTemplateFactory : FieldTemplateFactory { 13 private static bool? _directoryExists; 14 SimpleFieldTemplateFactory()15 public SimpleFieldTemplateFactory() 16 : this(HostingEnvironment.VirtualPathProvider) { 17 } 18 SimpleFieldTemplateFactory(VirtualPathProvider vpp)19 internal SimpleFieldTemplateFactory(VirtualPathProvider vpp) 20 : base(vpp) { 21 VirtualPathProvider = vpp; 22 } 23 24 internal VirtualPathProvider VirtualPathProvider { 25 get; 26 set; 27 } 28 29 protected virtual bool DirectoryExists { 30 get { 31 if (!_directoryExists.HasValue) { 32 // This is expensive so cache it. 33 _directoryExists = VirtualPathProvider.DirectoryExists(TemplateFolderVirtualPath); 34 } 35 return _directoryExists.Value; 36 } 37 } 38 CreateFieldTemplate(MetaColumn column, DataBoundControlMode mode, string uiHint)39 public override IFieldTemplate CreateFieldTemplate(MetaColumn column, DataBoundControlMode mode, string uiHint) { 40 // Call Preprocess mode so that we do set the right mode base on the the column's attributes 41 mode = PreprocessMode(column, mode); 42 bool readOnly = (mode == DataBoundControlMode.ReadOnly); 43 // If the folder doesn't exist use the fallback 44 if (!DirectoryExists) { 45 return CreateFieldTemplate(readOnly, column); 46 } 47 48 // Always see check if the base found anything first then fall back to the simple field template 49 IFieldTemplate fieldTemplate = base.CreateFieldTemplate(column, mode, uiHint); 50 51 // If there was no field template found and the user specified a uiHint then use the default behavior 52 if (!String.IsNullOrEmpty(uiHint)) { 53 return fieldTemplate; 54 } 55 56 return fieldTemplate ?? CreateFieldTemplate(readOnly, column); 57 } 58 CreateFieldTemplate(bool readOnly, MetaColumn column)59 private IFieldTemplate CreateFieldTemplate(bool readOnly, MetaColumn column) { 60 // By default we'll support checkbox fields for boolean and a textbox for 61 // everything else 62 if (column.ColumnType == typeof(bool)) { 63 return SimpleFieldTemplateUserControl.CreateBooleanTemplate(readOnly); 64 } 65 return SimpleFieldTemplateUserControl.CreateTextTemplate(column, readOnly); 66 } 67 } 68 } 69