2 // System.Web.UI.WebControls.TextBox.cs
5 // Ben Maurer (bmaurer@novell.com)
7 // (C) 2005 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Collections.Specialized;
30 using System.ComponentModel;
31 using System.Security.Permissions;
34 namespace System.Web.UI.WebControls {
37 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40 [DataBindingHandler ("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
41 [DefaultEvent ("TextChanged")]
42 [DefaultProperty ("Text")]
43 [ValidationProperty ("Text")]
44 [ControlBuilder (typeof (TextBoxControlBuilder))]
46 [Designer ("System.Web.UI.Design.WebControls.PreviewControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
47 [ParseChildren (true, "Text")]
48 [ControlValueProperty ("Text", null)]
49 [SupportsEventValidation]
51 [ParseChildren (false)]
53 public class TextBox : WebControl, IPostBackDataHandler
55 , IEditableTextControl, ITextControl
59 readonly static string [] VCardValues = new string [] {
74 "vCard.Home.StreetAddress",
83 "vCard.Business.City",
86 "vCard.Business.Phone",
87 "vCard.Business.State",
88 "vCard.Business.StreetAddress",
90 "vCard.Business.ZipCode",
95 protected override void AddAttributesToRender (HtmlTextWriter w)
99 page.VerifyRenderingInServerForm (this);
102 case TextBoxMode.MultiLine:
104 w.AddAttribute (HtmlTextWriterAttribute.Cols, Columns.ToString (), false);
107 w.AddAttribute (HtmlTextWriterAttribute.Cols, "20", false);
111 w.AddAttribute (HtmlTextWriterAttribute.Rows, Rows.ToString (), false);
114 w.AddAttribute (HtmlTextWriterAttribute.Rows, "2", false);
118 w.AddAttribute (HtmlTextWriterAttribute.Wrap, "off", false);
122 case TextBoxMode.SingleLine:
123 case TextBoxMode.Password:
125 if (TextMode == TextBoxMode.Password)
126 w.AddAttribute (HtmlTextWriterAttribute.Type, "password", false);
128 w.AddAttribute (HtmlTextWriterAttribute.Type, "text", false);
130 w.AddAttribute (HtmlTextWriterAttribute.Value, Text);
134 w.AddAttribute (HtmlTextWriterAttribute.Size, Columns.ToString (), false);
137 w.AddAttribute (HtmlTextWriterAttribute.Maxlength, MaxLength.ToString (), false);
140 if (AutoCompleteType != AutoCompleteType.None && TextMode == TextBoxMode.SingleLine)
141 if (AutoCompleteType != AutoCompleteType.Disabled)
142 w.AddAttribute (HtmlTextWriterAttribute.VCardName, VCardValues [(int) AutoCompleteType]);
144 w.AddAttribute (HtmlTextWriterAttribute.AutoComplete, "off", false);
151 w.AddAttribute ("onkeypress", "if (WebForm_TextBoxKeyHandler(event) == false) return false;", false);
154 string onchange = page.ClientScript.GetPostBackEventReference (GetPostBackOptions (), true);
155 onchange = String.Concat ("setTimeout('", onchange.Replace ("\\", "\\\\").Replace ("'", "\\'"), "', 0)");
156 w.AddAttribute (HtmlTextWriterAttribute.Onchange, BuildScriptAttribute ("onchange", onchange));
158 } else if (page != null)
159 page.ClientScript.RegisterForEventValidation (UniqueID, String.Empty);
162 if (page != null && AutoPostBack)
163 w.AddAttribute (HtmlTextWriterAttribute.Onchange,
164 BuildScriptAttribute ("onchange",
165 page.ClientScript.GetPostBackClientHyperlink (this, "")));
169 w.AddAttribute (HtmlTextWriterAttribute.ReadOnly, "ReadOnly", false);
171 w.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
173 base.AddAttributesToRender (w);
176 protected override void AddParsedSubObject (object obj)
178 LiteralControl l = obj as LiteralControl;
188 override void OnPreRender (EventArgs e)
190 // What do i do here?
191 base.OnPreRender (e);
194 RegisterKeyHandlerClientScript ();
198 if (page != null && Enabled)
199 page.RegisterEnabledControl (this);
208 override void Render (HtmlTextWriter w)
210 // Why didn't msft just override RenderContents!?
212 if (TextMode == TextBoxMode.MultiLine)
213 HttpUtility.HtmlEncode (Text, w);
220 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
223 ValidateEvent (postDataKey, String.Empty);
225 if (Text != postCollection [postDataKey]) {
226 Text = postCollection [postDataKey];
236 void RaisePostDataChangedEvent ()
239 if (CausesValidation)
240 Page.Validate (ValidationGroup);
242 OnTextChanged (EventArgs.Empty);
245 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
247 return LoadPostData (postDataKey, postCollection);
250 void IPostBackDataHandler.RaisePostDataChangedEvent ()
252 RaisePostDataChangedEvent ();
256 protected override object SaveViewState ()
258 if (TextMode == TextBoxMode.Password)
259 ViewState.SetItemDirty ("Text", false);
260 return base.SaveViewState ();
265 PostBackOptions GetPostBackOptions () {
266 PostBackOptions options = new PostBackOptions (this);
267 options.ActionUrl = null;
268 options.ValidationGroup = null;
269 options.Argument = String.Empty;
270 options.RequiresJavaScriptProtocol = false;
271 options.ClientSubmit = true;
274 options.PerformValidation = CausesValidation && page != null && page.AreValidatorsUplevel (ValidationGroup);
275 if (options.PerformValidation)
276 options.ValidationGroup = ValidationGroup;
281 void RegisterKeyHandlerClientScript () {
283 if (!Page.ClientScript.IsClientScriptBlockRegistered (typeof (TextBox), "KeyHandler")) {
284 StringBuilder script=new StringBuilder();
285 script.AppendLine ("function WebForm_TextBoxKeyHandler(event) {");
286 script.AppendLine ("\tvar target = event.target;");
287 script.AppendLine ("\tif ((target == null) || (typeof(target) == \"undefined\")) target = event.srcElement;");
288 script.AppendLine ("\tif (event.keyCode == 13) {");
289 script.AppendLine ("\t\tif ((typeof(target) != \"undefined\") && (target != null)) {");
290 script.AppendLine ("\t\t\tif (typeof(target.onchange) != \"undefined\") {");
291 script.AppendLine ("\t\t\t\ttarget.onchange();");
292 script.AppendLine ("\t\t\t\tevent.cancelBubble = true;");
293 script.AppendLine ("\t\t\t\tif (event.stopPropagation) event.stopPropagation();");
294 script.AppendLine ("\t\t\t\treturn false;");
295 script.AppendLine ("\t\t\t}");
296 script.AppendLine ("\t\t}");
297 script.AppendLine ("\t}");
298 script.AppendLine ("\treturn true;");
299 script.AppendLine ("}");
300 Page.ClientScript.RegisterClientScriptBlock (typeof (TextBox), "KeyHandler", script.ToString(), true);
306 [DefaultValue (AutoCompleteType.None)]
308 public virtual AutoCompleteType AutoCompleteType
311 object o = ViewState ["AutoCompleteType"];
312 return o != null ? (AutoCompleteType) o : AutoCompleteType.None;
315 ViewState ["AutoCompleteType"] = value;
320 [DefaultValue(false)]
324 [WebSysDescription ("")]
325 [WebCategory ("Behavior")]
326 public virtual bool AutoPostBack {
328 return ViewState.GetBool ("AutoPostBack", false);
331 ViewState ["AutoPostBack"] = value;
336 [DefaultValue (false)]
338 public virtual bool CausesValidation
341 return ViewState.GetBool ("CausesValidation", false);
344 ViewState["CausesValidation"] = value;
353 [WebSysDescription ("")]
354 [WebCategory ("Appearance")]
355 public virtual int Columns {
357 return ViewState.GetInt ("Columns", 0);
361 throw new ArgumentOutOfRangeException("value", "Columns value has to be 0 for 'not set' or bigger than 0.");
363 ViewState ["Columns"] = value;
374 [WebSysDescription ("")]
375 [WebCategory ("Behavior")]
376 public virtual int MaxLength {
378 return ViewState.GetInt ("MaxLength", 0);
382 throw new ArgumentOutOfRangeException("value", "MaxLength value has to be 0 for 'not set' or bigger than 0.");
384 ViewState ["MaxLength"] = value;
389 [DefaultValue(false)]
393 [WebSysDescription ("")]
394 [WebCategory ("Behavior")]
395 public virtual bool ReadOnly {
397 return ViewState.GetBool ("ReadOnly", false);
400 ViewState ["ReadOnly"] = value;
411 [WebSysDescription ("")]
412 [WebCategory ("Behavior")]
413 public virtual int Rows {
415 return ViewState.GetInt ("Rows", 0);
419 throw new ArgumentOutOfRangeException("value", "Rows value has to be 0 for 'not set' or bigger than 0.");
421 ViewState ["Rows"] = value;
425 #if NET_2_0 && HAVE_CONTROL_ADAPTERS
426 protected virtual new
430 HtmlTextWriterTag TagKey {
432 return TextMode == TextBoxMode.MultiLine ? HtmlTextWriterTag.Textarea : HtmlTextWriterTag.Input;
437 [Bindable(true, BindingDirection.TwoWay)]
442 [PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)]
445 [Editor ("System.ComponentModel.Design.MultilineStringEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
447 [WebSysDescription ("")]
448 [WebCategory ("Appearance")]
449 public virtual string Text {
451 return ViewState.GetString ("Text", "");
454 ViewState ["Text"] = value;
456 if (TextMode == TextBoxMode.Password)
457 ViewState.SetItemDirty ("Text", false);
462 [DefaultValue(TextBoxMode.SingleLine)]
466 [WebSysDescription ("")]
467 [WebCategory ("Behavior")]
468 public virtual TextBoxMode TextMode {
470 return (TextBoxMode) ViewState.GetInt ("TextMode", (int) TextBoxMode.SingleLine);
473 ViewState ["TextMode"] = (int) value;
480 public virtual string ValidationGroup
483 return ViewState.GetString ("ValidationGroup", "");
486 ViewState ["ValidationGroup"] = value;
492 [WebSysDescription ("")]
493 [WebCategory ("Layout")]
494 public virtual bool Wrap {
496 return ViewState.GetBool ("Wrap", true);
499 ViewState ["Wrap"] = value;
503 protected virtual void OnTextChanged (EventArgs e)
505 EventHandler h = (EventHandler) Events [TextChangedEvent];
510 static readonly object TextChangedEvent = new object ();
512 [WebSysDescription ("")]
513 [WebCategory ("Action")]
514 public event EventHandler TextChanged {
515 add { Events.AddHandler (TextChangedEvent, value); }
516 remove { Events.RemoveHandler (TextChangedEvent, value); }