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

TextBox.cs « System.Web.UI.WebControls « System.Web « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5d18246ae0212000e75f82e33e49dc16e9a37fe6 (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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//
// System.Web.UI.WebControls.TextBox.cs
//
// Authors:
//	Ben Maurer (bmaurer@novell.com)
//
// (C) 2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System.Collections.Specialized;
using System.ComponentModel;
using System.Security.Permissions;
using System.Text;

namespace System.Web.UI.WebControls {

	// CAS
	[AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
	[AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
	// attributes
	[DataBindingHandler ("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
	[DefaultEvent ("TextChanged")]
	[DefaultProperty ("Text")]
	[ValidationProperty ("Text")]
	[ControlBuilder (typeof (TextBoxControlBuilder))]
#if NET_2_0
	[Designer ("System.Web.UI.Design.WebControls.PreviewControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
	[ParseChildren (true, "Text")]
	[ControlValueProperty ("Text", null)]
	[SupportsEventValidation]
#else
	[ParseChildren (false)]
#endif		
	public class TextBox : WebControl, IPostBackDataHandler
#if NET_2_0
	, IEditableTextControl, ITextControl
#endif		  
	{
#if NET_2_0
		readonly static string [] VCardValues = new string [] {
			null,
			null,
			"vCard.Cellular",
			"vCard.Company",
			"vCard.Department",
			"vCard.DisplayName",
			"vCard.Email",
			"vCard.FirstName",
			"vCard.Gender",
			"vCard.Home.City",
			"HomeCountry",
			"vCard.Home.Fax",
			"vCard.Home.Phone",
			"vCard.Home.State",
			"vCard.Home.StreetAddress",
			"vCard.Home.ZipCode",
			"vCard.Home.page",
			"vCard.JobTitle",
			"vCard.LastName",
			"vCard.MiddleName",
			"vCard.Notes",
			"vCard.Office",
			"vCard.Pager",
			"vCard.Business.City",
			"BusinessCountry",
			"vCard.Business.Fax",
			"vCard.Business.Phone",
			"vCard.Business.State",
			"vCard.Business.StreetAddress",
			"vCard.Business.Url",
			"vCard.Business.ZipCode",
			"search"
		};
#endif

		protected override void AddAttributesToRender (HtmlTextWriter w)
		{
			if (Page != null)
				Page.VerifyRenderingInServerForm (this);

			base.AddAttributesToRender (w);

			if (ToolTip.Length > 0)
				w.AddAttribute (HtmlTextWriterAttribute.Title, ToolTip);
			
			switch (TextMode) {
			case TextBoxMode.MultiLine:
				if (Columns != 0)
					w.AddAttribute (HtmlTextWriterAttribute.Cols, Columns.ToString ());
#if NET_2_0
				else
					w.AddAttribute (HtmlTextWriterAttribute.Cols, "20");
#endif
				
				if (Rows != 0)
					w.AddAttribute (HtmlTextWriterAttribute.Rows, Rows.ToString ());
#if NET_2_0
				else
					w.AddAttribute (HtmlTextWriterAttribute.Rows, "2");
#endif

				if (!Wrap)
					w.AddAttribute (HtmlTextWriterAttribute.Wrap, "off");
				
				break;
				
			case TextBoxMode.SingleLine:
			case TextBoxMode.Password:
				
				if (TextMode == TextBoxMode.Password)
					w.AddAttribute (HtmlTextWriterAttribute.Type, "password");
				else {
					w.AddAttribute (HtmlTextWriterAttribute.Type, "text");
					if (Text.Length > 0)
						w.AddAttribute (HtmlTextWriterAttribute.Value, Text);
				}
				
				if (Columns != 0)
					w.AddAttribute (HtmlTextWriterAttribute.Size, Columns.ToString ());
		
				if (MaxLength != 0)
					w.AddAttribute (HtmlTextWriterAttribute.Maxlength, MaxLength.ToString ());

#if NET_2_0
				if (AutoCompleteType != AutoCompleteType.None && TextMode == TextBoxMode.SingleLine)
					if (AutoCompleteType != AutoCompleteType.Disabled)
						w.AddAttribute (HtmlTextWriterAttribute.VCardName, VCardValues [(int) AutoCompleteType]);
					else
						w.AddAttribute (HtmlTextWriterAttribute.AutoComplete, "off");
#endif
				break;	
			}

#if NET_2_0
			if (AutoPostBack) {
				w.AddAttribute ("onkeypress", "if (WebForm_TextBoxKeyHandler(event) == false) return false;");
				w.AddAttribute (HtmlTextWriterAttribute.Onchange, Page.ClientScript.GetPostBackEventReference (GetPostBackOptions (), true));
			}
#else		
			if (AutoPostBack)
				w.AddAttribute (HtmlTextWriterAttribute.Onchange, Page.ClientScript.GetPostBackClientHyperlink (this, ""));
#endif

			if (ReadOnly)
				w.AddAttribute (HtmlTextWriterAttribute.ReadOnly, "ReadOnly");

			w.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
		}

		protected override void AddParsedSubObject (object obj)
		{
			LiteralControl l = obj as LiteralControl;
			if (l != null)
				Text = l.Text;
		}

#if NET_2_0
		protected internal
#else		
		protected
#endif		
		override void OnPreRender (EventArgs e)
		{
			// What do i do here?
			base.OnPreRender (e);
#if NET_2_0
			if (AutoPostBack) {
				RegisterKeyHandlerClientScript ();
			}
#endif
		}

#if NET_2_0
		protected internal
#else		
		protected
#endif		
		override void Render (HtmlTextWriter w)
		{
			// Why didn't msft just override RenderContents!?
			RenderBeginTag (w);
			if (TextMode == TextBoxMode.MultiLine)
				HttpUtility.HtmlEncode (Text, w);
			RenderEndTag (w);
		}
		
#if NET_2_0
		protected virtual
#endif
		bool LoadPostData (string postDataKey, NameValueCollection postCollection)
		{
			if (Text != postCollection [postDataKey]) {
				Text = postCollection [postDataKey];
				return true;
			}
			
			return false;
		}

#if NET_2_0
		protected virtual
#endif
		void RaisePostDataChangedEvent ()
		{
#if NET_2_0
			if (CausesValidation)
				Page.Validate (ValidationGroup);
#endif
			OnTextChanged (EventArgs.Empty);
		}

		bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
		{
			return LoadPostData (postDataKey, postCollection);
		}
	
		void IPostBackDataHandler.RaisePostDataChangedEvent ()
		{
			RaisePostDataChangedEvent ();
		}

#if NET_2_0
		protected override object SaveViewState ()
		{
			return base.SaveViewState ();
		}
#endif		
	
#if NET_2_0
		PostBackOptions GetPostBackOptions () {
			PostBackOptions options = new PostBackOptions (this);
			options.ActionUrl = null;
			options.ValidationGroup = null;
			options.Argument = "";
			options.RequiresJavaScriptProtocol = false;
			options.ClientSubmit = true;
			options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
			if (options.PerformValidation)
				options.ValidationGroup = ValidationGroup;

			return options;
		}

		void RegisterKeyHandlerClientScript () {

			if (!Page.ClientScript.IsClientScriptBlockRegistered (typeof (TextBox), "KeyHandler")) {
				StringBuilder script=new StringBuilder();
				script.AppendLine ("function WebForm_TextBoxKeyHandler(event) {");
				script.AppendLine ("\tvar target = event.target;");
				script.AppendLine ("\tif ((target == null) || (typeof(target) == \"undefined\")) target = event.srcElement;");
				script.AppendLine ("\tif (event.keyCode == 13) {");
				script.AppendLine ("\t\tif ((typeof(target) != \"undefined\") && (target != null)) {");
				script.AppendLine ("\t\t\tif (typeof(target.onchange) != \"undefined\") {");
				script.AppendLine ("\t\t\t\ttarget.onchange();");
				script.AppendLine ("\t\t\t\tevent.cancelBubble = true;");
				script.AppendLine ("\t\t\t\tif (event.stopPropagation) event.stopPropagation();");
				script.AppendLine ("\t\t\t\treturn false;");
				script.AppendLine ("\t\t\t}");
				script.AppendLine ("\t\t}");
				script.AppendLine ("\t}");
				script.AppendLine ("\treturn true;");
				script.AppendLine ("}");
				Page.ClientScript.RegisterClientScriptBlock (typeof (TextBox), "KeyHandler", script.ToString(), true);
			}
		}
#endif

#if NET_2_0
		[DefaultValue (AutoCompleteType.None)]
		[Themeable (false)]
		public virtual AutoCompleteType AutoCompleteType 
		{
			get {
				object o = ViewState ["AutoCompleteType"];
				return o != null ? (AutoCompleteType) o : AutoCompleteType.None;
			}
			set {
				ViewState ["AutoCompleteType"] = value;
			}
		}
#endif		
		
		[DefaultValue(false)]
#if NET_2_0
		[Themeable (false)]
#endif		
		[WebSysDescription ("")]
		[WebCategory ("Behavior")]
		public virtual bool AutoPostBack {
			get {
				return ViewState.GetBool ("AutoPostBack", false);
			}
			set {
				ViewState ["AutoPostBack"] = value;
			}
		}

#if NET_2_0
		[DefaultValue (false)]
		[Themeable (false)]
		public virtual bool CausesValidation
		{
			get {
				return ViewState.GetBool ("CausesValidation", false);
			}
			set {
				ViewState["CausesValidation"] = value;
			}
		}
#endif		

#if ONLY_1_1
		[Bindable(true)]
#endif		
		[DefaultValue(0)]
		[WebSysDescription ("")]
		[WebCategory ("Appearance")]
		public virtual int Columns {
			get {
				return ViewState.GetInt ("Columns", 0);
			}
			set {
				if (value < 0)
					throw new ArgumentOutOfRangeException("value", "Columns value has to be 0 for 'not set' or bigger than 0.");
				else
					ViewState ["Columns"] = value;
			}
		}

#if ONLY_1_1
		[Bindable(true)]
#endif		
		[DefaultValue(0)]
#if NET_2_0
		[Themeable (false)]
#endif
		[WebSysDescription ("")]
		[WebCategory ("Behavior")]
		public virtual int MaxLength {
			get {
				return ViewState.GetInt ("MaxLength", 0);
			}
			set {
				if (value < 0)
					throw new ArgumentOutOfRangeException("value", "MaxLength value has to be 0 for 'not set' or bigger than 0.");
				else
					ViewState ["MaxLength"] = value;
			}
		}

		[Bindable(true)]
		[DefaultValue(false)]
#if NET_2_0
		[Themeable (false)]
#endif
		[WebSysDescription ("")]
		[WebCategory ("Behavior")]
		public virtual bool ReadOnly {
			get {
				return ViewState.GetBool ("ReadOnly", false);
			}
			set {
				ViewState ["ReadOnly"] = value;
			}
		}

#if ONLY_1_1
		[Bindable(true)]
#endif		
		[DefaultValue(0)]
#if NET_2_0
		[Themeable (false)]
#endif
		[WebSysDescription ("")]
		[WebCategory ("Behavior")]
		public virtual int Rows {
			get {
				return ViewState.GetInt ("Rows", 0);
			}
			set {
				if (value < 0)
					throw new ArgumentOutOfRangeException("value", "Rows value has to be 0 for 'not set' or bigger than 0.");
				else
					ViewState ["Rows"] = value;
			}
		}
	
#if NET_2_0 && HAVE_CONTROL_ADAPTERS
		protected virtual new
#else		
		protected override
#endif
		HtmlTextWriterTag TagKey {
			get {
				return TextMode == TextBoxMode.MultiLine ? HtmlTextWriterTag.Textarea : HtmlTextWriterTag.Input;
			}
		}

#if NET_2_0
		[Bindable(true, BindingDirection.TwoWay)]
#else
		[Bindable(true)]
#endif		
		[DefaultValue("")]
		[PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)]
#if NET_2_0
		[Localizable (true)]
		[Editor ("System.ComponentModel.Design.MultilineStringEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
#endif
		[WebSysDescription ("")]
		[WebCategory ("Appearance")]
		public virtual string Text {
			get {
				return ViewState.GetString ("Text", "");
			}
			set {
				ViewState ["Text"] = value;
				if (TextMode == TextBoxMode.Password)
					ViewState.SetItemDirty ("Text", false);
			}
		}
	
		[DefaultValue(TextBoxMode.SingleLine)]
#if NET_2_0
		[Themeable (false)]
#endif
		[WebSysDescription ("")]
		[WebCategory ("Behavior")]
		public virtual TextBoxMode TextMode {
			get {
				return (TextBoxMode) ViewState.GetInt ("TextMode", (int) TextBoxMode.SingleLine);
			}
			set {
				ViewState ["TextMode"] = (int) value;
			}
		}

#if NET_2_0
		[Themeable (false)]
		[DefaultValue ("")]
		public virtual string ValidationGroup
		{
			get {
				return ViewState.GetString ("ValidationGroup", "");
			}
			set {
				ViewState ["ValidationGroup"] = value;
			}
		}
#endif		
	
		[DefaultValue(true)]
		[WebSysDescription ("")]
		[WebCategory ("Layout")]
		public virtual bool Wrap {
			get {
				return ViewState.GetBool ("Wrap", true);
			}
			set {
				ViewState ["Wrap"] = value;
			}
		}

		protected virtual void OnTextChanged (EventArgs e)
		{
			EventHandler h = (EventHandler) Events [TextChangedEvent];
			if (h != null)
				h (this, e);
		}
		
		static readonly object TextChangedEvent = new object ();

		[WebSysDescription ("")]
		[WebCategory ("Action")]
		public event EventHandler TextChanged {
			add { Events.AddHandler (TextChangedEvent, value); }
			remove { Events.RemoveHandler (TextChangedEvent, value); }
		}
	}
}