1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
namespace System.Web.DynamicData {
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Collections;
using System.Collections.Specialized;
// Dynamically creates a field template for text or boolean fields
internal class SimpleFieldTemplateUserControl : FieldTemplateUserControl {
private const string TextBoxID = "TextBox";
// Used for edit scenariors, since we only have one value for these
// simple fieldtemplates
private Func<object> _valueExtrator;
private List<BaseValidator> _validators;
protected SimpleFieldTemplateUserControl() {
}
protected override void OnInit(EventArgs e) {
base.OnInit(e);
// Add the validators to the field template
InitializeValidators();
}
protected override void ExtractValues(IOrderedDictionary dictionary) {
if (_valueExtrator != null) {
object value = _valueExtrator();
string stringValue = value as string;
dictionary[Column.Name] = (stringValue != null ? ConvertEditedValue(stringValue) : value);
}
}
public static SimpleFieldTemplateUserControl CreateBooleanTemplate(bool readOnly) {
SimpleFieldTemplateUserControl control = new SimpleFieldTemplateUserControl();
var checkBox = new CheckBox();
checkBox.Enabled = !readOnly;
checkBox.DataBinding += (sender, e) => {
if (control.FieldValue != null) {
checkBox.Checked = (bool)control.FieldValue;
}
};
if (!readOnly) {
control._valueExtrator = () => checkBox.Checked;
}
control.Controls.Add(checkBox);
return control;
}
public static SimpleFieldTemplateUserControl CreateTextTemplate(MetaColumn column, bool readOnly) {
SimpleFieldTemplateUserControl control = new SimpleFieldTemplateUserControl();
if (readOnly) {
var literal = new Literal();
literal.DataBinding += (sender, e) => {
literal.Text = control.FieldValueString;
};
control.Controls.Add(literal);
}
else {
var textBox = new TextBox();
textBox.DataBinding += (sender, e) => {
textBox.Text = control.FieldValueEditString;
};
// Logic copied from BoundField
if (column.ColumnType.IsPrimitive) {
textBox.Columns = 5;
}
control._valueExtrator = () => textBox.Text;
textBox.CssClass = "DDTextBox";
textBox.ID = TextBoxID;
control.Controls.Add(textBox);
control.CreateValidators(column);
}
return control;
}
internal void InitializeValidators() {
if (_validators != null) {
_validators.ForEach(v => Controls.Add(v));
_validators.ForEach(v => SetUpValidator(v));
}
}
// This method create's validators for a particular column type. This should be as close to the the actual FieldTemplates (user controls) as possible.
// DateTime -> Required, Regex
// Integer -> Regex, Required, Range, Compare
// Decimal -> Regex, Required, Range, Compare
// Text -> Regex, Required
// Enum -> Required
private void CreateValidators(MetaColumn column) {
if (_validators == null) {
_validators = new List<BaseValidator>();
}
// Exclude regular expression validator for enum columns
if (column.GetEnumType() == null) {
RegularExpressionValidator regularExpressionValidator = new RegularExpressionValidator {
ControlToValidate = TextBoxID,
Enabled = false,
Display = ValidatorDisplay.Static,
CssClass = "DDControl DDValidator"
};
_validators.Add(regularExpressionValidator);
}
if (column.IsInteger || column.ColumnType == typeof(decimal) || column.ColumnType == typeof(double) || column.ColumnType == typeof(float)) {
RangeValidator rangeValidator = new RangeValidator {
ControlToValidate = TextBoxID,
Enabled = false,
Display = ValidatorDisplay.Static,
MinimumValue = "0",
MaximumValue = "100",
CssClass = "DDControl DDValidator",
Type = column.IsInteger ? ValidationDataType.Integer : ValidationDataType.Double
};
_validators.Add(rangeValidator);
CompareValidator compareValidator = new CompareValidator {
ControlToValidate = TextBoxID,
Enabled = false,
Display = ValidatorDisplay.Static,
Operator = ValidationCompareOperator.DataTypeCheck,
CssClass = "DDControl DDValidator",
Type = column.IsInteger ? ValidationDataType.Integer : ValidationDataType.Double
};
_validators.Add(compareValidator);
}
RequiredFieldValidator requiredFieldValidator = new RequiredFieldValidator {
ControlToValidate = TextBoxID,
Enabled = false,
CssClass = "DDControl DDValidator",
Display = ValidatorDisplay.Static
};
_validators.Add(requiredFieldValidator);
DynamicValidator dynamicValidator = new DynamicValidator {
ControlToValidate = TextBoxID,
CssClass = "DDControl DDValidator",
Display = ValidatorDisplay.Static
};
_validators.Add(dynamicValidator);
}
}
}
|