2004-05-26 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / TextBox.cs
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
10 //
11
12 using System;
13 using System.Collections.Specialized;
14 using System.ComponentModel;
15 using System.Globalization;
16 using System.Web;
17 using System.Web.UI;
18
19 namespace System.Web.UI.WebControls
20 {
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
28         {
29                 static readonly object TextChangedEvent = new object ();
30
31                 public TextBox() : base (HtmlTextWriterTag.Input)
32                 {
33                 }
34
35                 [DefaultValue (false), WebCategory ("Behavior")]
36                 [WebSysDescription ("The control automatically posts back after changing the text.")]
37                 public virtual bool AutoPostBack {
38                         get {
39                                 object o = ViewState ["AutoPostBack"];
40                                 return (o == null) ? false : (bool) o;
41                         }
42
43                         set { ViewState ["AutoPostBack"] = value; }
44                 }
45
46                 [DefaultValue (0), Bindable (true), WebCategory ("Appearance")]
47                 [WebSysDescription ("The width of this control specified in characters.")]
48                 public virtual int Columns {
49                         get {
50                                 object o = ViewState ["Columns"];
51                                 return (o == null) ? 0 : (int) o;
52                         }
53
54                         set {
55                                 if (value < 0)
56                                         throw new ArgumentOutOfRangeException ("value",
57                                                 "Columns value has to be 0 for 'not set' or bigger than 0.");
58
59                                 ViewState ["Columns"] = value; 
60                         }
61                 }
62
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 {
66                         get {
67                                 object o = ViewState ["MaxLength"];
68                                 return (o == null) ? 0 : (int) o;
69                         }
70
71                         set {
72                                 if (value < 0)
73                                         throw new ArgumentOutOfRangeException ("value",
74                                                 "MaxLength value has to be 0 for 'not set' or bigger than 0.");
75
76                                 ViewState ["MaxLength"] = value;
77                         }
78                 }
79
80                 [DefaultValue (false), Bindable (true), WebCategory ("Behavior")]
81                 [WebSysDescription ("If the control is ReadOnly you cannot enter new text.")]
82                 public virtual bool ReadOnly {
83                         get {
84                                 object o = ViewState ["ReadOnly"];
85                                 return (o == null) ? false : (bool) o;
86                         }
87                         set { ViewState ["ReadOnly"] = value; }
88                 }
89
90                 [DefaultValue (0), Bindable (true), WebCategory ("Behavior")]
91                 [WebSysDescription ("The number of lines that this multiline contol spans.")]
92                 public virtual int Rows {
93                         get {
94                                 object o = ViewState ["Rows"];
95                                 return (o == null) ? 0 : (int) o;
96                         }
97
98                         set {
99                                 if (value < 0)
100                                         throw new ArgumentOutOfRangeException ("value",
101                                                 "Rows value has to be 0 for 'not set' or bigger than 0.");
102                                 ViewState ["Rows"] = value;
103                         }
104                 }
105
106                 [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
107                 [PersistenceMode (PersistenceMode.EncodedInnerDefaultProperty)]
108                 [WebSysDescription ("The text that this control initially displays.")]
109                 public virtual string Text {
110                         get {
111                                 object o = ViewState ["Text"];
112                                 return (o == null) ? String.Empty : (string) o;
113                         }
114
115                         set { ViewState ["Text"] = value; }
116                 }
117
118                 [DefaultValue (typeof (TextBoxMode), "SingleLine"), WebCategory ("Behavior")]
119                 [WebSysDescription ("A mode of how the control operates.")]
120                 public virtual TextBoxMode TextMode {
121                         get {
122                                 object o = ViewState ["TextMode"];
123                                 return (o == null) ? TextBoxMode.SingleLine : (TextBoxMode) o;
124                         }
125
126                         set {
127                                 if(!Enum.IsDefined (typeof(TextBoxMode), value))
128                                         throw new ArgumentOutOfRangeException ("value",
129                                                                 "Only existing modes are allowed");
130
131                                 ViewState ["TextMode"] = value;
132                         }
133                 }
134
135                 [DefaultValue (true), WebCategory ("Layout")]
136                 [WebSysDescription ("Determines if a line wraps at line-end.")]
137                 public virtual bool Wrap {
138                         get {
139                                 object o = ViewState ["Wrap"];
140                                 return (o == null) ? true : (bool) o;
141                         }
142
143                         set { ViewState ["Wrap"] = value; }
144                 }
145
146
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); }
152                 }
153
154                 protected override HtmlTextWriterTag TagKey {
155                         get {
156                                 if(TextMode == TextBoxMode.MultiLine)
157                                         return HtmlTextWriterTag.Textarea;
158                                 return HtmlTextWriterTag.Input;
159                         }
160                 }
161
162                 protected override void AddAttributesToRender (HtmlTextWriter writer)
163                 {
164                         if(Page != null)
165                                 Page.VerifyRenderingInServerForm (this);
166
167                         NumberFormatInfo invar = NumberFormatInfo.InvariantInfo;
168
169                         writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
170                         if (TextMode == TextBoxMode.MultiLine) {
171                                 if (Rows > 0)
172                                         writer.AddAttribute (HtmlTextWriterAttribute.Rows,
173                                                              Rows.ToString (invar));
174
175                                 if (Columns > 0)
176                                         writer.AddAttribute (HtmlTextWriterAttribute.Cols,
177                                                              Columns.ToString (invar));
178
179                                 if (!Wrap)
180                                         writer.AddAttribute(HtmlTextWriterAttribute.Wrap, "off");
181                         } else {
182                                 string mode;
183                                 if (TextMode == TextBoxMode.Password) {
184                                         mode = "password";
185                                 } else {
186                                         mode = "text";
187                                         if (Text.Length > 0)
188                                                 writer.AddAttribute (HtmlTextWriterAttribute.Value, Text);
189                                 }
190                                         
191                                 writer.AddAttribute (HtmlTextWriterAttribute.Type, mode);
192                                 if (MaxLength > 0)
193                                         writer.AddAttribute (HtmlTextWriterAttribute.Maxlength,
194                                                              MaxLength.ToString (invar));
195
196                                 if (Columns > 0)
197                                         writer.AddAttribute (HtmlTextWriterAttribute.Size,
198                                                              Columns.ToString (invar));
199                         }
200
201                         if (ReadOnly)
202                                 writer.AddAttribute (HtmlTextWriterAttribute.ReadOnly, "readonly");
203
204                         base.AddAttributesToRender (writer);
205
206                         if (AutoPostBack && Page != null){
207                                 writer.AddAttribute (HtmlTextWriterAttribute.Onchange,
208                                                      Page.GetPostBackClientEvent (this, ""));
209                                 writer.AddAttribute ("language", "javascript");
210                         }
211                 }
212
213                 protected override void AddParsedSubObject(object obj)
214                 {
215                         if(!(obj is LiteralControl))
216                                 throw new HttpException ("Cannot have children of type" + obj.GetType ());
217
218                         Text = ((LiteralControl) obj).Text;
219                 }
220
221                 protected override void OnPreRender (EventArgs e)
222                 {
223                         base.OnPreRender (e);
224                         bool enabled = Enabled;
225                         if (Page != null) {
226                                 if (AutoPostBack && enabled)
227                                         Page.RequiresPostBackScript ();
228                         }
229
230                         /* Don't save passwords in ViewState */
231                         if (TextMode == TextBoxMode.Password ||
232                             (enabled && Visible && Events [TextChangedEvent] == null))
233                                 ViewState.SetItemDirty ("Text", false);
234                 }
235
236                 protected virtual void OnTextChanged (EventArgs e)
237                 {
238                         if(Events != null){
239                                 EventHandler eh = (EventHandler) (Events [TextChangedEvent]);
240                                 if(eh != null)
241                                         eh (this, e);
242                         }
243                 }
244
245                 protected override void Render (HtmlTextWriter writer)
246                 {
247                         RenderBeginTag(writer);
248                         if (TextMode == TextBoxMode.MultiLine)
249                                 HttpUtility.HtmlEncode (Text, writer);
250                         RenderEndTag(writer);
251                 }
252
253                 bool IPostBackDataHandler.LoadPostData (string postDataKey,
254                                                         NameValueCollection postCollection)
255                 {
256                         if (postCollection [postDataKey] != Text){
257                                 Text = postCollection [postDataKey];
258                                 return true;
259                         }
260                         return false;
261                 }
262
263                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
264                 {
265                         OnTextChanged (EventArgs.Empty);
266                 }
267         }
268 }
269