**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Web / System.Web.UI.WebControls / TextBox.cs
blob6622c262c24b199424947722258e06e3cabf7d2f
1 //
2 // System.Web.UI.WebControls.TextBox.cs
3 //
4 // Authors:
5 // Gaurav Vaish (gvaish@iitk.ac.in)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) Gaurav Vaish (2002)
9 // (C) 2003 Andreas Nahr
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System;
34 using System.Collections.Specialized;
35 using System.ComponentModel;
36 using System.Globalization;
37 using System.Web;
38 using System.Web.UI;
40 namespace System.Web.UI.WebControls
42 [ControlBuilder (typeof (TextBoxControlBuilder))]
43 [DefaultEvent("TextChanged")]
44 [DefaultProperty("Text")]
45 [ParseChildren(false)]
46 [ValidationProperty("Text")]
47 [DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
48 public class TextBox : WebControl, IPostBackDataHandler
50 static readonly object TextChangedEvent = new object ();
52 public TextBox() : base (HtmlTextWriterTag.Input)
56 [DefaultValue (false), WebCategory ("Behavior")]
57 [WebSysDescription ("The control automatically posts back after changing the text.")]
58 public virtual bool AutoPostBack {
59 get {
60 object o = ViewState ["AutoPostBack"];
61 return (o == null) ? false : (bool) o;
64 set { ViewState ["AutoPostBack"] = value; }
67 [DefaultValue (0), Bindable (true), WebCategory ("Appearance")]
68 [WebSysDescription ("The width of this control specified in characters.")]
69 public virtual int Columns {
70 get {
71 object o = ViewState ["Columns"];
72 return (o == null) ? 0 : (int) o;
75 set {
76 if (value < 0)
77 throw new ArgumentOutOfRangeException ("value",
78 "Columns value has to be 0 for 'not set' or bigger than 0.");
80 ViewState ["Columns"] = value;
84 [DefaultValue (0), Bindable (true), WebCategory ("Behavior")]
85 [WebSysDescription ("The maximum number of characters you can enter in this control.")]
86 public virtual int MaxLength {
87 get {
88 object o = ViewState ["MaxLength"];
89 return (o == null) ? 0 : (int) o;
92 set {
93 if (value < 0)
94 throw new ArgumentOutOfRangeException ("value",
95 "MaxLength value has to be 0 for 'not set' or bigger than 0.");
97 ViewState ["MaxLength"] = value;
101 [DefaultValue (false), Bindable (true), WebCategory ("Behavior")]
102 [WebSysDescription ("If the control is ReadOnly you cannot enter new text.")]
103 public virtual bool ReadOnly {
104 get {
105 object o = ViewState ["ReadOnly"];
106 return (o == null) ? false : (bool) o;
108 set { ViewState ["ReadOnly"] = value; }
111 [DefaultValue (0), Bindable (true), WebCategory ("Behavior")]
112 [WebSysDescription ("The number of lines that this multiline contol spans.")]
113 public virtual int Rows {
114 get {
115 object o = ViewState ["Rows"];
116 return (o == null) ? 0 : (int) o;
119 set {
120 if (value < 0)
121 throw new ArgumentOutOfRangeException ("value",
122 "Rows value has to be 0 for 'not set' or bigger than 0.");
123 ViewState ["Rows"] = value;
127 [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
128 [PersistenceMode (PersistenceMode.EncodedInnerDefaultProperty)]
129 [WebSysDescription ("The text that this control initially displays.")]
130 public virtual string Text {
131 get {
132 object o = ViewState ["Text"];
133 return (o == null) ? String.Empty : (string) o;
136 set { ViewState ["Text"] = value; }
139 [DefaultValue (typeof (TextBoxMode), "SingleLine"), WebCategory ("Behavior")]
140 [WebSysDescription ("A mode of how the control operates.")]
141 public virtual TextBoxMode TextMode {
142 get {
143 object o = ViewState ["TextMode"];
144 return (o == null) ? TextBoxMode.SingleLine : (TextBoxMode) o;
147 set {
148 if(!Enum.IsDefined (typeof(TextBoxMode), value))
149 throw new ArgumentOutOfRangeException ("value",
150 "Only existing modes are allowed");
152 ViewState ["TextMode"] = value;
156 [DefaultValue (true), WebCategory ("Layout")]
157 [WebSysDescription ("Determines if a line wraps at line-end.")]
158 public virtual bool Wrap {
159 get {
160 object o = ViewState ["Wrap"];
161 return (o == null) ? true : (bool) o;
164 set { ViewState ["Wrap"] = value; }
168 [WebCategory ("Action")]
169 [WebSysDescription ("Raised when the text is changed.")]
170 public event EventHandler TextChanged {
171 add { Events.AddHandler (TextChangedEvent, value); }
172 remove { Events.RemoveHandler (TextChangedEvent, value); }
175 protected override HtmlTextWriterTag TagKey {
176 get {
177 if(TextMode == TextBoxMode.MultiLine)
178 return HtmlTextWriterTag.Textarea;
179 return HtmlTextWriterTag.Input;
183 protected override void AddAttributesToRender (HtmlTextWriter writer)
185 if(Page != null)
186 Page.VerifyRenderingInServerForm (this);
188 NumberFormatInfo invar = NumberFormatInfo.InvariantInfo;
190 writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
191 if (TextMode == TextBoxMode.MultiLine) {
192 if (Rows > 0)
193 writer.AddAttribute (HtmlTextWriterAttribute.Rows,
194 Rows.ToString (invar));
196 if (Columns > 0)
197 writer.AddAttribute (HtmlTextWriterAttribute.Cols,
198 Columns.ToString (invar));
200 if (!Wrap)
201 writer.AddAttribute(HtmlTextWriterAttribute.Wrap, "off");
202 } else {
203 string mode;
204 if (TextMode == TextBoxMode.Password) {
205 mode = "password";
206 } else {
207 mode = "text";
208 if (Text.Length > 0)
209 writer.AddAttribute (HtmlTextWriterAttribute.Value, Text);
212 writer.AddAttribute (HtmlTextWriterAttribute.Type, mode);
213 if (MaxLength > 0)
214 writer.AddAttribute (HtmlTextWriterAttribute.Maxlength,
215 MaxLength.ToString (invar));
217 if (Columns > 0)
218 writer.AddAttribute (HtmlTextWriterAttribute.Size,
219 Columns.ToString (invar));
222 if (ReadOnly)
223 writer.AddAttribute (HtmlTextWriterAttribute.ReadOnly, "readonly");
225 base.AddAttributesToRender (writer);
227 if (AutoPostBack && Page != null){
228 writer.AddAttribute (HtmlTextWriterAttribute.Onchange,
229 Page.GetPostBackClientEvent (this, ""));
230 writer.AddAttribute ("language", "javascript");
234 protected override void AddParsedSubObject(object obj)
236 if(!(obj is LiteralControl))
237 throw new HttpException ("Cannot have children of type" + obj.GetType ());
239 Text = ((LiteralControl) obj).Text;
242 protected override void OnPreRender (EventArgs e)
244 base.OnPreRender (e);
245 bool enabled = Enabled;
246 if (Page != null) {
247 if (AutoPostBack && enabled)
248 Page.RequiresPostBackScript ();
251 /* Don't save passwords in ViewState */
252 if (TextMode == TextBoxMode.Password ||
253 (enabled && Visible && Events [TextChangedEvent] == null))
254 ViewState.SetItemDirty ("Text", false);
257 protected virtual void OnTextChanged (EventArgs e)
259 if(Events != null){
260 EventHandler eh = (EventHandler) (Events [TextChangedEvent]);
261 if(eh != null)
262 eh (this, e);
266 protected override void Render (HtmlTextWriter writer)
268 RenderBeginTag(writer);
269 if (TextMode == TextBoxMode.MultiLine)
270 HttpUtility.HtmlEncode (Text, writer);
271 RenderEndTag(writer);
274 bool IPostBackDataHandler.LoadPostData (string postDataKey,
275 NameValueCollection postCollection)
277 if (postCollection [postDataKey] != Text){
278 Text = postCollection [postDataKey];
279 return true;
281 return false;
284 void IPostBackDataHandler.RaisePostDataChangedEvent ()
286 OnTextChanged (EventArgs.Empty);