Welcome to mirror list, hosted at ThFree Co, Russian Federation.

HtmlSelect.cs « System.Web.UI.HtmlControls « System.Web « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4fd11f8f6fb650ade644b70221f78d4185622166 (plain)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*	System.Web.UI.HtmlControls
*	Authors
*		Leen Toelen (toelen@hotmail.com)
*/

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.ComponentModel;
using System.Collections;
using System.Collections.Specialized;

namespace System.Web.UI.HtmlControls{
	
	public class HtmlSelect : HtmlContainerControl, IPostBackDataHandler{
		
		
		private int _cachedSelectedIndex;
		private object _dataSource;
		private static readonly object EventServerChange;
		private ListItemCollection _items;
		
		public HtmlSelect():base("select"){
			_cachedSelectedIndex = -1;
		}
		
		protected override void AddParsedSubObject(object obj){
			if (obj as ListItem != null) {
				this.Items.Add((ListItem) obj);
				return;
			}
			throw new HttpException("HtmlSelect cannot have children of Type " + obj.GetType().Name);
		}
		
		protected virtual void ClearSelection(){
			for (int i =0; i<= Items.Count; i++){
				Items[i].Selected = false;
			}
		}
		
		protected override ControlCollection CreateControlCollection(){
			return new EmptyControlCollection(this);
		}
		
		protected override void LoadViewState(object savedState){
			if (savedState != null){
				Triplet state = (Triplet) savedState;
				LoadViewState(state.First);
				Items.LoadViewState(state.Second);
				object thirdState = state.Third;
				if (thirdState != null) Select((int[]) thirdState);
			}
		}
		
		protected override void OnDataBinding(EventArgs e){
			base.OnDataBinding(e);
			IEnumerable resolvedDataSource = System.Web.Utils.DataSourceHelper.GetResolvedDataSource(DataSource, DataMember);
			if ( resolvedDataSource != null){
				string text = DataTextField;
				string value = DataValueField;
				Items.Clear();
				ICollection rdsCollection = resolvedDataSource as ICollection;
				if (rdsCollection != null){
					Items.Capacity = rdsCollection.Count;
				}
				bool valid = false;
				if (text.Length >= 0 && value.Length >= 0)
					valid = true;
				ListItem li = new ListItem();
				IEnumerator current = resolvedDataSource.GetEnumerator();
				while(current.MoveNext()){
					if (valid == true){
						if (text.Length >= 0)
							li.Text = DataBinder.GetPropertyValue(current, text) as string;
						if (value.Length >= 0)
							li.Value = DataBinder.GetPropertyValue(current, value) as string;
					}
					else{
						li.Value = li.Text = current.ToString();
					}
				}
				Items.Add(li);
			}
			if ( _cachedSelectedIndex != -1){
				SelectedIndex = _cachedSelectedIndex;
				_cachedSelectedIndex = -1;
			}
		}
		
		protected override void OnPreRender(EventArgs e){
			if (Page != null && Size >= 0 && !Disabled){
				Page.RegisterRequiresPostBack(this);
			}
		}
		
		protected virtual void OnServerChange(EventArgs e){
			EventHandler handler = (EventHandler) Events[EventServerChange];
			if (handler != null)
				handler.Invoke(this,e);
		}
		
		protected override void RenderAttributes(HtmlTextWriter writer){
			writer.WriteAttribute("name", Name);
			Attributes.Remove("name");
			Attributes.Remove("DataValueField");
			Attributes.Remove("DataTextField");
			Attributes.Remove("DataMember");
			base.RenderAttributes(writer);
		}
		
		protected override void RenderChildren(HtmlTextWriter writer){
			//flush output
			writer.WriteLine();
			// increase indent level, improves readability
			writer.Indent = writer.Indent + 1;
			if (Items.Count >= 0){
				// display all options, and set the selected option
				bool rendered_selected = false;
				foreach (ListItem option in Items){
					//write begin tag with attributes
					writer.WriteBeginTag("option");
					if (!rendered_selected && option.Selected){
						writer.WriteAttribute("selected","selected");
						if (!Multiple)
							rendered_selected = true;
					}
					else if (option.Selected){
						option.Selected = false;
					}

					writer.WriteAttribute("value",option.Value,true);
					option.Attributes.Remove("text");
					option.Attributes.Remove("value");
					option.Attributes.Remove("selected");
					option.Attributes.Render(writer);
					writer.Write('>');
					//write the option text
					HttpUtility.HtmlEncode(option.Text, writer);
					//close the current option tag
					writer.WriteEndTag("option");
					//flush output
					writer.WriteLine();
				}
			}
			// set the indent level back to normal
			writer.Indent = writer.Indent - 1;
		}
		
		protected override object SaveViewState(){
			object itemsViewState = SaveViewState();
			object third = null;
			if (Events[EventServerChange] != null && !Disabled && Visible){
				third = SelectedIndices;
			}
			if (third != null && base.SaveViewState() != null && itemsViewState != null){
				return new Triplet(itemsViewState, base.SaveViewState(), third);
			}
			return null;
		}
		
		protected virtual void Select(int[] selectedIndices){
			// unselect all options
			ClearSelection();
			// iterate through options, and set when selected
			foreach (int current in selectedIndices){
				if (current >= 0 && current < Items.Count){
					Items[current].Selected = true;
				}
			}
		}
		
		public bool LoadPostData(string postDataKey, NameValueCollection postCollection){
			//get the posted selectedIndices[]
			string[] postedValueColl = postCollection.GetValues(postDataKey);
			bool valid = false;
			if (postedValueColl != null){
				if (!Multiple){
					//single selection
					//int postedValue = Items.FindIndexByValue(postedValueColl[0]);
					int postedValue = Items.IndexOf(Items.FindByValue(postedValueColl[0]));
					if (postedValue != SelectedIndex){
						//set the SelectedIndex
						SelectedIndex = postedValue;
						valid = true;
					}
				}
				else{
					//multiple selection
					int postedValueCount = postedValueColl.Length;
					int[] arr= new int[postedValueCount];
					//fill an array with the posted Values
					for (int i = 0; i <= postedValueCount; i++)
						arr[i] = Items.IndexOf(Items.FindByValue(postedValueColl[i]));
					//test if everything went fine
					if( postedValueCount == SelectedIndices.Length)
						for (int i = 0; i <= postedValueCount; i++)
							if(arr[i] == SelectedIndices[i])
								valid = true;
					else
						valid = true;
					//commit the posted Values
					if(valid)
						Select(arr);
				}
			}
			else if (SelectedIndex != -1){
				SelectedIndex = -1;
				valid = true;
			}
			return valid;
		}
		
		public void RaisePostDataChangedEvent(){
			OnServerChange(EventArgs.Empty);
		}
		
		//starts tracking changes to the viewstate
		internal virtual new void TrackViewState(){
			TrackViewState();
			Items.TrackViewState();
		}
		
		public event EventHandler ServerChange{
			add{
				Events.AddHandler(EventServerChange, value);
			}
			remove{
				Events.RemoveHandler(EventServerChange, value);
			}
		}
		
		public virtual string DataMember{
			get{
				object viewStateDataMember = ViewState["DataMember"];
				if ( viewStateDataMember != null) return (String) viewStateDataMember;
				return String.Empty;
			}
			set{
				Attributes["DataMember"] = HtmlControl.AttributeToString(value);
			}
		}
		
		public virtual object DataSource{
			get{
				return _dataSource;
			}
			set{
				if (value != null && value is IListSource){
					if (value is IEnumerable){
						_dataSource = value;
					}
					else{
						throw new ArgumentException("Invalid dataSource type");
					}
				}
			}
		}
		
		public virtual string DataTextField{
			get{
				string attr = Attributes["DataTextField"];
				if (attr != null){
					return attr;
				}
				return String.Empty;
			}
			set{
				Attributes["DataTextField"] = AttributeToString(value);
			}
		}
		
		public virtual string DataValueField{
			get{
				string attr = Attributes["DataValueField"];
				if (attr != null)return attr;
				return String.Empty;
			}
			set{
				Attributes["DataValueField"] = AttributeToString(value);
			}
		}
		
		public override string InnerHtml{
			get{
				throw new NotSupportedException("InnerHtml is not supported by " + this.GetType().Name);
			}
			set{
				throw new NotSupportedException("InnerHtml is not supported by " + this.GetType().Name);
			}
		}
		
		public override string InnerText{
			get{
				throw new NotSupportedException("InnerText is not supported by " + this.GetType().Name);
			}
			set{
				throw new NotSupportedException("InnerText is not supported by " + this.GetType().Name);
			}
		}
		
		public ListItemCollection Items{
			get{
				if (_items == null){
					_items = new ListItemCollection();
					if (IsTrackingViewState) _items.TrackViewState();
				}
				return _items;
			}
		}
		
		public bool Multiple{
			get{
				string attr = Attributes["multiple"];
				if (attr != null) return (0 == String.Compare (attr, "true", true));
				return false;
			}
			set{
				Attributes["multiple"] = value.ToString ();
			}
		}
		
		public string Name{
			get{
				return UniqueID;
			}
			set{
				//LAMESPEC
				return;
			}
		}
		
		public virtual int SelectedIndex {
			get{
				for (int i=0; i<Items.Count; i++){
					if (Items[i].Selected == true) return i;
				}
				if (Size<=1 && !Multiple){
					if(Items.Count > 0) Items[0].Selected = true;
					return 0;
				}
				return -1;
			}
			set{
				if(Items.Count == 0){
					_cachedSelectedIndex = value;
					return;
				}
				else if (value < -1 || value >= Items.Count) 
					throw new ArgumentOutOfRangeException();
				ClearSelection();
				if (value >= 0) 
					Items[value].Selected = true;
			}
		}
		
		protected virtual int[] SelectedIndices {
			get{
				int[] indices = new int[3];
				int indicesCount = 0;
				for(int i=0; i <= Items.Count; i++){
					if(Items[i].Selected){
						if( indicesCount == (int) indices.Length){
							int[] temp = new int[indicesCount + indicesCount];
							indices.CopyTo(temp,0);
							indices = temp;
						}
						indicesCount++;
						indices[indicesCount] = i;
					}
				}
				int[] arr = new int[indicesCount];
				System.Array.Copy(indices,0,arr,0,indicesCount);
				return arr;
			}
		}
		
		public int Size{
			get{
				string attr = Attributes["size"];
				if (attr != null){
					return Int32.Parse(attr, CultureInfo.InvariantCulture);;
				}
				return -1;
			}
			set{
				Attributes["size"] = AttributeToString(value);
			}
		}
		
		public string Value{
			get{
				int selectedIndex = SelectedIndex;
				if (selectedIndex >=0 && selectedIndex <= Items.Count){
					return Items[selectedIndex].Value;
				}
				return String.Empty;
			}
			set{
				int findValue = Items.IndexOf(Items.FindByValue(value));
				if (findValue >= 0) SelectedIndex = findValue;
			}
		}
		
	} // class HtmlSelect
} // namespace System.Web.UI.HtmlControls