Ewl » Canonical The canonical version of EWL Read More
Clone URL:  
BlockCheckBox.cs
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using EnterpriseWebLibrary.EnterpriseWebFramework.Controls; using EnterpriseWebLibrary.EnterpriseWebFramework.DisplayLinking; using EnterpriseWebLibrary.InputValidation; namespace EnterpriseWebLibrary.EnterpriseWebFramework { [ Obsolete( "Guaranteed through 28 Feb 2019. Use Checkbox instead." ) ] [ ParseChildren( ChildrenAsProperties = true, DefaultProperty = "NestedControls" ) ] public class BlockCheckBox: WebControl, CommonCheckBox, ControlTreeDataLoader, FormValueControl, ControlWithJsInitLogic, FormControl<FlowComponent> { private readonly FormValue<bool> checkBoxFormValue; private readonly FormValue<CommonCheckBox> radioButtonFormValue; private readonly string radioButtonListItemId; private readonly IEnumerable<PhrasingComponent> label; private readonly BlockCheckBoxSetup setup; private readonly FormAction action; private readonly List<Func<IEnumerable<string>>> jsClickHandlerStatementLists = new List<Func<IEnumerable<string>>>(); private readonly EwfValidation validation; private readonly IReadOnlyCollection<Control> nestedControls; private WebControl checkBox; /// <summary> /// Creates a checkbox. /// </summary> /// <param name="isChecked"></param> /// <param name="label">The checkbox label. Do not pass null. Pass an empty collection for no label.</param> /// <param name="setup">The setup object for the checkbox.</param> /// <param name="validationMethod">The validation method. Pass null if you’re only using this control for page modification.</param> public BlockCheckBox( bool isChecked, IEnumerable<PhrasingComponent> label, BlockCheckBoxSetup setup = null, Action<PostBackValue<bool>, Validator> validationMethod = null ) { Labeler = new FormControlLabeler(); this.setup = setup ?? new BlockCheckBoxSetup(); checkBoxFormValue = EwfCheckBox.GetFormValue( isChecked, this ); this.label = label; action = this.setup.Action ?? FormState.Current.DefaultAction; if( validationMethod != null ) validation = checkBoxFormValue.CreateValidation( validationMethod ); nestedControls = this.setup.NestedControlListGetter?.Invoke().ToImmutableArray() ?? ImmutableArray<Control>.Empty; } /// <summary> /// Creates a radio button. /// </summary> internal BlockCheckBox( FormValue<CommonCheckBox> formValue, BlockCheckBoxSetup setup, IEnumerable<PhrasingComponent> label, Func<IEnumerable<string>> jsClickHandlerStatementListGetter, Action<PostBackValue<bool>, Validator> validationMethod, string listItemId = null ) { Labeler = new FormControlLabeler(); radioButtonFormValue = formValue; radioButtonListItemId = listItemId; this.label = label; this.setup = setup; action = setup.Action ?? FormState.Current.DefaultAction; jsClickHandlerStatementLists.Add( jsClickHandlerStatementListGetter ); if( validationMethod != null ) validation = formValue.CreateValidation( ( postBackValue, validator ) => validationMethod( new PostBackValue<bool>( postBackValue.Value == this, postBackValue.ChangedOnPostBack ), validator ) ); nestedControls = setup.NestedControlListGetter?.Invoke().ToImmutableArray() ?? ImmutableArray<Control>.Empty; } public FormControlLabeler Labeler { get; } FlowComponent FormControl<FlowComponent>.PageComponent { get { throw new ApplicationException( "not implemented" ); } } string CommonCheckBox.GroupName { get { return checkBoxFormValue != null ? "" : ( (FormValue)radioButtonFormValue ).GetPostBackValueKey(); } } /// <summary> /// EWF ToolTip to display on this control. Setting ToolTipControl will ignore this property. /// </summary> public override string ToolTip { get; set; } /// <summary> /// Control to display inside the tool tip. Do not pass null. This will ignore the ToolTip property. /// </summary> public Control ToolTipControl { get; set; } /// <summary> /// Adds a javascript method to be called when the check box is clicked. Example: AddOnClickJsMethod( "changeCheckBoxColor( this )" ). /// </summary> public void AddOnClickJsMethod( string jsMethodInvocation ) { jsClickHandlerStatementLists.Add( jsMethodInvocation.ToCollection ); } public EwfValidation Validation { get { return validation; } } public bool IsRadioButton { get { return radioButtonFormValue != null; } } /// <summary> /// Gets whether the box was created in a checked state. /// </summary> public bool IsChecked { get { return checkBoxFormValue != null ? checkBoxFormValue.GetDurableValue() : radioButtonFormValue.GetDurableValue() == this; } } void ControlTreeDataLoader.LoadData() { action.AddToPageIfNecessary(); PreRender += delegate { if( setup.HighlightedWhenChecked && checkBoxFormValue.GetValue( AppRequestState.Instance.EwfPageRequestState.PostBackValues ) ) CssClass = CssClass.ConcatenateWithSpace( "checkedChecklistCheckboxDiv" ); }; var table = TableOps.CreateUnderlyingTable(); table.CssClass = "ewfBlockCheckBox"; checkBox = new WebControl( HtmlTextWriterTag.Input ); PreRender += delegate { EwfCheckBox.AddCheckBoxAttributes( checkBox, this, checkBoxFormValue, radioButtonFormValue, radioButtonListItemId, action, setup.TriggersPostBackWhenCheckedOrUnchecked, jsClickHandlerStatementLists.SelectMany( i => i() ) ); }; var checkBoxCell = new TableCell().AddControlsReturnThis( checkBox ); checkBoxCell.Style.Add( "width", "13px" ); var row = new TableRow(); row.Cells.Add( checkBoxCell ); var labelControl = new HtmlGenericControl( "label" ).AddControlsReturnThis( label.GetControls() ); row.Cells.Add( new TableCell().AddControlsReturnThis( labelControl ) ); PreRender += ( s, e ) => labelControl.Attributes.Add( "for", checkBox.ClientID ); table.Rows.Add( row ); if( nestedControls.Any() ) { var nestedControlRow = new TableRow(); nestedControlRow.Cells.Add( new TableCell() ); nestedControlRow.Cells.Add( new TableCell().AddControlsReturnThis( nestedControls ) ); table.Rows.Add( nestedControlRow ); if( !setup.NestedControlsAlwaysVisible ) CheckBoxToControlArrayDisplayLink.AddToPage( this, true, nestedControlRow ); } Controls.Add( table ); if( ToolTip != null || ToolTipControl != null ) new Controls.ToolTip( ToolTipControl ?? EnterpriseWebFramework.Controls.ToolTip.GetToolTipTextControl( ToolTip ), label.Any() ? (Control)labelControl : checkBox ); Labeler.AddControlId( checkBox.ClientID ); } string ControlWithJsInitLogic.GetJsInitStatements() { return setup.HighlightedWhenChecked ? "$( '#" + checkBox.ClientID + "' ).click( function() { changeCheckBoxColor( this ); } );" : ""; } FormValue FormValueControl.FormValue { get { return (FormValue)checkBoxFormValue ?? radioButtonFormValue; } } /// <summary> /// Gets whether the box is checked in the post back. /// </summary> public bool IsCheckedInPostBack( PostBackValueDictionary postBackValues ) { return checkBoxFormValue != null ? checkBoxFormValue.GetValue( postBackValues ) : radioButtonFormValue.GetValue( postBackValues ) == this; } /// <summary> /// Returns true if the value changed on this post back. /// </summary> public bool ValueChangedOnPostBack( PostBackValueDictionary postBackValues ) { return checkBoxFormValue != null ? checkBoxFormValue.ValueChangedOnPostBack( postBackValues ) : radioButtonFormValue.ValueChangedOnPostBack( postBackValues ); } /// <summary> /// Returns the div tag, which represents this control in HTML. /// </summary> protected override HtmlTextWriterTag TagKey { get { return HtmlTextWriterTag.Div; } } } }