2 // System.Web.UI.WebControls.TextBox.cs
5 // Gaurav Vaish (gvaish@iitk.ac.in)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 // (C) Gaurav Vaish (2002)
9 // (C) 2003 Andreas Nahr
13 using System.Collections.Specialized;
14 using System.ComponentModel;
15 using System.Globalization;
19 namespace System.Web.UI.WebControls
21 [ControlBuilder (typeof (TextBoxControlBuilder))]
22 [DefaultEvent("TextChanged")]
23 [DefaultProperty("Text")]
24 [ParseChildren(false)]
25 [ValidationProperty("Text")]
26 [DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
27 public class TextBox : WebControl, IPostBackDataHandler
29 static readonly object TextChangedEvent = new object ();
31 public TextBox() : base (HtmlTextWriterTag.Input)
35 [DefaultValue (false), WebCategory ("Behavior")]
36 [WebSysDescription ("The control automatically posts back after changing the text.")]
37 public virtual bool AutoPostBack {
39 object o = ViewState ["AutoPostBack"];
40 return (o == null) ? false : (bool) o;
43 set { ViewState ["AutoPostBack"] = value; }
46 [DefaultValue (0), Bindable (true), WebCategory ("Appearance")]
47 [WebSysDescription ("The width of this control specified in characters.")]
48 public virtual int Columns {
50 object o = ViewState ["Columns"];
51 return (o == null) ? 0 : (int) o;
56 throw new ArgumentOutOfRangeException ("value",
57 "Columns value has to be 0 for 'not set' or bigger than 0.");
59 ViewState ["Columns"] = value;
63 [DefaultValue (0), Bindable (true), WebCategory ("Behavior")]
64 [WebSysDescription ("The maximum number of characters you can enter in this control.")]
65 public virtual int MaxLength {
67 object o = ViewState ["MaxLength"];
68 return (o == null) ? 0 : (int) o;
73 throw new ArgumentOutOfRangeException ("value",
74 "MaxLength value has to be 0 for 'not set' or bigger than 0.");
76 ViewState ["MaxLength"] = value;
80 [DefaultValue (false), Bindable (true), WebCategory ("Behavior")]
81 [WebSysDescription ("If the control is ReadOnly you cannot enter new text.")]
82 public virtual bool ReadOnly {
84 object o = ViewState ["ReadOnly"];
85 return (o == null) ? false : (bool) o;
87 set { ViewState ["ReadOnly"] = value; }
90 [DefaultValue (0), Bindable (true), WebCategory ("Behavior")]
91 [WebSysDescription ("The number of lines that this multiline contol spans.")]
92 public virtual int Rows {
94 object o = ViewState ["Rows"];
95 return (o == null) ? 0 : (int) o;
100 throw new ArgumentOutOfRangeException ("value",
101 "Rows value has to be 0 for 'not set' or bigger than 0.");
102 ViewState ["Rows"] = value;
106 [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
107 [PersistenceMode (PersistenceMode.EncodedInnerDefaultProperty)]
108 [WebSysDescription ("The text that this control initially displays.")]
109 public virtual string Text {
111 object o = ViewState ["Text"];
112 return (o == null) ? String.Empty : (string) o;
115 set { ViewState ["Text"] = value; }
118 [DefaultValue (typeof (TextBoxMode), "SingleLine"), WebCategory ("Behavior")]
119 [WebSysDescription ("A mode of how the control operates.")]
120 public virtual TextBoxMode TextMode {
122 object o = ViewState ["TextMode"];
123 return (o == null) ? TextBoxMode.SingleLine : (TextBoxMode) o;
127 if(!Enum.IsDefined (typeof(TextBoxMode), value))
128 throw new ArgumentOutOfRangeException ("value",
129 "Only existing modes are allowed");
131 ViewState ["TextMode"] = value;
135 [DefaultValue (true), WebCategory ("Layout")]
136 [WebSysDescription ("Determines if a line wraps at line-end.")]
137 public virtual bool Wrap {
139 object o = ViewState ["Wrap"];
140 return (o == null) ? true : (bool) o;
143 set { ViewState ["Wrap"] = value; }
147 [WebCategory ("Action")]
148 [WebSysDescription ("Raised when the text is changed.")]
149 public event EventHandler TextChanged {
150 add { Events.AddHandler (TextChangedEvent, value); }
151 remove { Events.RemoveHandler (TextChangedEvent, value); }
154 protected override HtmlTextWriterTag TagKey {
156 if(TextMode == TextBoxMode.MultiLine)
157 return HtmlTextWriterTag.Textarea;
158 return HtmlTextWriterTag.Input;
162 protected override void AddAttributesToRender (HtmlTextWriter writer)
165 Page.VerifyRenderingInServerForm (this);
167 NumberFormatInfo invar = NumberFormatInfo.InvariantInfo;
169 writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
170 if (TextMode == TextBoxMode.MultiLine) {
172 writer.AddAttribute (HtmlTextWriterAttribute.Rows,
173 Rows.ToString (invar));
176 writer.AddAttribute (HtmlTextWriterAttribute.Cols,
177 Columns.ToString (invar));
180 writer.AddAttribute(HtmlTextWriterAttribute.Wrap, "off");
183 if (TextMode == TextBoxMode.Password) {
188 writer.AddAttribute (HtmlTextWriterAttribute.Value, Text);
191 writer.AddAttribute (HtmlTextWriterAttribute.Type, mode);
193 writer.AddAttribute (HtmlTextWriterAttribute.Maxlength,
194 MaxLength.ToString (invar));
197 writer.AddAttribute (HtmlTextWriterAttribute.Size,
198 Columns.ToString (invar));
202 writer.AddAttribute (HtmlTextWriterAttribute.ReadOnly, "readonly");
204 base.AddAttributesToRender (writer);
206 if (AutoPostBack && Page != null){
207 writer.AddAttribute (HtmlTextWriterAttribute.Onchange,
208 Page.GetPostBackClientEvent (this, ""));
209 writer.AddAttribute ("language", "javascript");
213 protected override void AddParsedSubObject(object obj)
215 if(!(obj is LiteralControl))
216 throw new HttpException ("Cannot have children of type" + obj.GetType ());
218 Text = ((LiteralControl) obj).Text;
221 protected override void OnPreRender (EventArgs e)
223 base.OnPreRender (e);
224 bool enabled = Enabled;
226 if (AutoPostBack && enabled)
227 Page.RequiresPostBackScript ();
230 /* Don't save passwords in ViewState */
231 if (TextMode == TextBoxMode.Password ||
232 (enabled && Visible && Events [TextChangedEvent] == null))
233 ViewState.SetItemDirty ("Text", false);
236 protected virtual void OnTextChanged (EventArgs e)
239 EventHandler eh = (EventHandler) (Events [TextChangedEvent]);
245 protected override void Render (HtmlTextWriter writer)
247 RenderBeginTag(writer);
248 if (TextMode == TextBoxMode.MultiLine)
249 HttpUtility.HtmlEncode (Text, writer);
250 RenderEndTag(writer);
253 bool IPostBackDataHandler.LoadPostData (string postDataKey,
254 NameValueCollection postCollection)
256 if (postCollection [postDataKey] != Text){
257 Text = postCollection [postDataKey];
263 void IPostBackDataHandler.RaisePostDataChangedEvent ()
265 OnTextChanged (EventArgs.Empty);