2005-08-29 Chris Toshok <toshok@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.HtmlControls / HtmlInputText.cs
1 //
2 // System.Web.UI.HtmlControls.HtmlInputText.cs
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28
29 using System.ComponentModel;
30 using System.Collections.Specialized;
31 using System.Globalization;
32
33 namespace System.Web.UI.HtmlControls {
34
35         [DefaultEvent ("ServerChange")]
36         [ValidationProperty ("Value")]
37         public class HtmlInputText : HtmlInputControl, IPostBackDataHandler {
38
39                 private static readonly object serverChangeEvent = new object ();
40
41
42                 public HtmlInputText ()
43                         : base ("text")
44                 {
45                 }
46
47                 public HtmlInputText (string type)
48                         : base (type)
49                 {
50                 }
51
52
53                 [DefaultValue ("")]
54                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
55                 [WebSysDescription("")]
56                 [WebCategory("Behavior")]
57                 public int MaxLength {
58                         get {
59                                 string s = Attributes ["maxlength"];
60                                 return (s == null) ? -1 : Convert.ToInt32 (s);
61                         }
62                         set {
63                                 if (value == -1)
64                                         Attributes.Remove ("maxlength");
65                                 else
66                                         Attributes ["maxlength"] = value.ToString ();
67                         }
68                 }
69
70 #if NET_2_0
71                 [DefaultValue (-1)]
72 #else
73                 [DefaultValue ("")]
74 #endif
75                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
76                 [WebSysDescription("")]
77                 [WebCategory("Appearance")]
78                 public int Size {
79                         get {
80                                 string s = Attributes ["size"];
81                                 return (s == null) ? -1 : Convert.ToInt32 (s);
82                         }
83                         set {
84                                 if (value == -1)
85                                         Attributes.Remove ("size");
86                                 else
87                                         Attributes ["size"] = value.ToString ();
88                         }
89                 }
90
91                 public override string Value {
92                         get {
93                                 string s = Attributes ["value"];
94                                 return (s == null) ? String.Empty : s;
95                         }
96                         set {
97                                 if (value == null)
98                                         Attributes.Remove ("value");
99                                 else
100                                         Attributes ["value"] = value;
101                         }
102                 }
103
104
105 #if NET_2_0
106                 protected internal
107 #else           
108                 protected
109 #endif          
110                 override void OnPreRender (EventArgs e)
111                 {
112                         base.OnPreRender (e);
113                 }
114
115                 protected virtual void OnServerChange (EventArgs e)
116                 {
117                         EventHandler serverChange = (EventHandler) Events [serverChangeEvent];
118                         if (serverChange != null)
119                                 serverChange (this, e);
120                 }
121 #if NET_2_0
122                 protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
123                 {
124                         return DefaultLoadPostData (postDataKey, postCollection);
125                 }
126
127                 protected virtual void RaisePostDataChangedEvent ()
128                 {
129                         OnServerChange (EventArgs.Empty);
130                 }
131 #else
132                 protected override void RenderAttributes (HtmlTextWriter writer)
133                 {
134                         // the Type property can be, indirectly, changed by using the Attributes property
135                         if (String.Compare (Type, 0, "password", 0, 8, true, CultureInfo.InvariantCulture) == 0) {
136                                 Attributes.Remove ("value");
137                         }
138
139                         base.RenderAttributes (writer);
140                 }
141 #endif
142
143                 internal bool DefaultLoadPostData (string postDataKey, NameValueCollection postCollection)
144                 {
145                         string s = postCollection [postDataKey];
146                         if (Attributes ["value"] != s) {
147                                 Attributes ["value"] = s;
148                                 return true;
149                         }
150                         return false;
151                 }
152
153                 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
154                 {
155 #if NET_2_0
156                         return LoadPostData (postDataKey, postCollection);
157 #else
158                         return DefaultLoadPostData (postDataKey, postCollection);
159 #endif
160                 }
161
162                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
163                 {
164 #if NET_2_0
165                         RaisePostDataChangedEvent ();
166 #else
167                         OnServerChange (EventArgs.Empty);
168 #endif
169                 }
170
171
172                 [WebSysDescription("")]
173                 [WebCategory("Action")]
174                 public event EventHandler ServerChange {
175                         add { Events.AddHandler (serverChangeEvent, value); }
176                         remove { Events.RemoveHandler (serverChangeEvent, value); }
177                 }
178         }
179 }