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)
98 Page.VerifyRenderingInServerForm (this);
101 case TextBoxMode.MultiLine:
103 w.AddAttribute (HtmlTextWriterAttribute.Cols, Columns.ToString (), false);
106 w.AddAttribute (HtmlTextWriterAttribute.Cols, "20", false);
110 w.AddAttribute (HtmlTextWriterAttribute.Rows, Rows.ToString (), false);
113 w.AddAttribute (HtmlTextWriterAttribute.Rows, "2", false);
117 w.AddAttribute (HtmlTextWriterAttribute.Wrap, "off", false);
121 case TextBoxMode.SingleLine:
122 case TextBoxMode.Password:
124 if (TextMode == TextBoxMode.Password)
125 w.AddAttribute (HtmlTextWriterAttribute.Type, "password", false);
127 w.AddAttribute (HtmlTextWriterAttribute.Type, "text", false);
129 w.AddAttribute (HtmlTextWriterAttribute.Value, Text);
133 w.AddAttribute (HtmlTextWriterAttribute.Size, Columns.ToString (), false);
136 w.AddAttribute (HtmlTextWriterAttribute.Maxlength, MaxLength.ToString (), false);
139 if (AutoCompleteType != AutoCompleteType.None && TextMode == TextBoxMode.SingleLine)
140 if (AutoCompleteType != AutoCompleteType.Disabled)
141 w.AddAttribute (HtmlTextWriterAttribute.VCardName, VCardValues [(int) AutoCompleteType]);
143 w.AddAttribute (HtmlTextWriterAttribute.AutoComplete, "off", false);
150 w.AddAttribute ("onkeypress", "if (WebForm_TextBoxKeyHandler(event) == false) return false;", false);
152 string onchange = Page.ClientScript.GetPostBackEventReference (GetPostBackOptions (), true);
153 onchange = String.Concat ("setTimeout('", onchange.Replace ("\\", "\\\\").Replace ("'", "\\'"), "', 0)");
154 w.AddAttribute (HtmlTextWriterAttribute.Onchange, BuildScriptAttribute ("onchange", onchange));
158 w.AddAttribute (HtmlTextWriterAttribute.Onchange,
159 BuildScriptAttribute ("onchange",
160 Page.ClientScript.GetPostBackClientHyperlink (this, "")));
164 w.AddAttribute (HtmlTextWriterAttribute.ReadOnly, "ReadOnly", false);
166 w.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
168 base.AddAttributesToRender (w);
171 protected override void AddParsedSubObject (object obj)
173 LiteralControl l = obj as LiteralControl;
183 override void OnPreRender (EventArgs e)
185 // What do i do here?
186 base.OnPreRender (e);
189 RegisterKeyHandlerClientScript ();
192 if (Page != null && Enabled)
193 Page.RegisterEnabledControl (this);
202 override void Render (HtmlTextWriter w)
204 // Why didn't msft just override RenderContents!?
206 if (TextMode == TextBoxMode.MultiLine)
207 HttpUtility.HtmlEncode (Text, w);
214 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
216 if (Text != postCollection [postDataKey]) {
217 Text = postCollection [postDataKey];
227 void RaisePostDataChangedEvent ()
230 if (CausesValidation)
231 Page.Validate (ValidationGroup);
233 OnTextChanged (EventArgs.Empty);
236 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
238 return LoadPostData (postDataKey, postCollection);
241 void IPostBackDataHandler.RaisePostDataChangedEvent ()
243 RaisePostDataChangedEvent ();
247 protected override object SaveViewState ()
249 if (TextMode == TextBoxMode.Password)
250 ViewState.SetItemDirty ("Text", false);
251 return base.SaveViewState ();
256 PostBackOptions GetPostBackOptions () {
257 PostBackOptions options = new PostBackOptions (this);
258 options.ActionUrl = null;
259 options.ValidationGroup = null;
260 options.Argument = "";
261 options.RequiresJavaScriptProtocol = false;
262 options.ClientSubmit = true;
263 options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
264 if (options.PerformValidation)
265 options.ValidationGroup = ValidationGroup;
270 void RegisterKeyHandlerClientScript () {
272 if (!Page.ClientScript.IsClientScriptBlockRegistered (typeof (TextBox), "KeyHandler")) {
273 StringBuilder script=new StringBuilder();
274 script.AppendLine ("function WebForm_TextBoxKeyHandler(event) {");
275 script.AppendLine ("\tvar target = event.target;");
276 script.AppendLine ("\tif ((target == null) || (typeof(target) == \"undefined\")) target = event.srcElement;");
277 script.AppendLine ("\tif (event.keyCode == 13) {");
278 script.AppendLine ("\t\tif ((typeof(target) != \"undefined\") && (target != null)) {");
279 script.AppendLine ("\t\t\tif (typeof(target.onchange) != \"undefined\") {");
280 script.AppendLine ("\t\t\t\ttarget.onchange();");
281 script.AppendLine ("\t\t\t\tevent.cancelBubble = true;");
282 script.AppendLine ("\t\t\t\tif (event.stopPropagation) event.stopPropagation();");
283 script.AppendLine ("\t\t\t\treturn false;");
284 script.AppendLine ("\t\t\t}");
285 script.AppendLine ("\t\t}");
286 script.AppendLine ("\t}");
287 script.AppendLine ("\treturn true;");
288 script.AppendLine ("}");
289 Page.ClientScript.RegisterClientScriptBlock (typeof (TextBox), "KeyHandler", script.ToString(), true);
295 [DefaultValue (AutoCompleteType.None)]
297 public virtual AutoCompleteType AutoCompleteType
300 object o = ViewState ["AutoCompleteType"];
301 return o != null ? (AutoCompleteType) o : AutoCompleteType.None;
304 ViewState ["AutoCompleteType"] = value;
309 [DefaultValue(false)]
313 [WebSysDescription ("")]
314 [WebCategory ("Behavior")]
315 public virtual bool AutoPostBack {
317 return ViewState.GetBool ("AutoPostBack", false);
320 ViewState ["AutoPostBack"] = value;
325 [DefaultValue (false)]
327 public virtual bool CausesValidation
330 return ViewState.GetBool ("CausesValidation", false);
333 ViewState["CausesValidation"] = value;
342 [WebSysDescription ("")]
343 [WebCategory ("Appearance")]
344 public virtual int Columns {
346 return ViewState.GetInt ("Columns", 0);
350 throw new ArgumentOutOfRangeException("value", "Columns value has to be 0 for 'not set' or bigger than 0.");
352 ViewState ["Columns"] = value;
363 [WebSysDescription ("")]
364 [WebCategory ("Behavior")]
365 public virtual int MaxLength {
367 return ViewState.GetInt ("MaxLength", 0);
371 throw new ArgumentOutOfRangeException("value", "MaxLength value has to be 0 for 'not set' or bigger than 0.");
373 ViewState ["MaxLength"] = value;
378 [DefaultValue(false)]
382 [WebSysDescription ("")]
383 [WebCategory ("Behavior")]
384 public virtual bool ReadOnly {
386 return ViewState.GetBool ("ReadOnly", false);
389 ViewState ["ReadOnly"] = value;
400 [WebSysDescription ("")]
401 [WebCategory ("Behavior")]
402 public virtual int Rows {
404 return ViewState.GetInt ("Rows", 0);
408 throw new ArgumentOutOfRangeException("value", "Rows value has to be 0 for 'not set' or bigger than 0.");
410 ViewState ["Rows"] = value;
414 #if NET_2_0 && HAVE_CONTROL_ADAPTERS
415 protected virtual new
419 HtmlTextWriterTag TagKey {
421 return TextMode == TextBoxMode.MultiLine ? HtmlTextWriterTag.Textarea : HtmlTextWriterTag.Input;
426 [Bindable(true, BindingDirection.TwoWay)]
431 [PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)]
434 [Editor ("System.ComponentModel.Design.MultilineStringEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
436 [WebSysDescription ("")]
437 [WebCategory ("Appearance")]
438 public virtual string Text {
440 return ViewState.GetString ("Text", "");
443 ViewState ["Text"] = value;
445 if (TextMode == TextBoxMode.Password)
446 ViewState.SetItemDirty ("Text", false);
451 [DefaultValue(TextBoxMode.SingleLine)]
455 [WebSysDescription ("")]
456 [WebCategory ("Behavior")]
457 public virtual TextBoxMode TextMode {
459 return (TextBoxMode) ViewState.GetInt ("TextMode", (int) TextBoxMode.SingleLine);
462 ViewState ["TextMode"] = (int) value;
469 public virtual string ValidationGroup
472 return ViewState.GetString ("ValidationGroup", "");
475 ViewState ["ValidationGroup"] = value;
481 [WebSysDescription ("")]
482 [WebCategory ("Layout")]
483 public virtual bool Wrap {
485 return ViewState.GetBool ("Wrap", true);
488 ViewState ["Wrap"] = value;
492 protected virtual void OnTextChanged (EventArgs e)
494 EventHandler h = (EventHandler) Events [TextChangedEvent];
499 static readonly object TextChangedEvent = new object ();
501 [WebSysDescription ("")]
502 [WebCategory ("Action")]
503 public event EventHandler TextChanged {
504 add { Events.AddHandler (TextChangedEvent, value); }
505 remove { Events.RemoveHandler (TextChangedEvent, value); }