svn path=/branches/mono-1-1-9/mcs/; revision=51216
[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 using System.Security.Permissions;
33
34 namespace System.Web.UI.HtmlControls {
35
36         // CAS
37         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39         // attributes
40         [DefaultEvent ("ServerChange")]
41         [ValidationProperty ("Value")]
42         public class HtmlInputText : HtmlInputControl, IPostBackDataHandler {
43
44                 private static readonly object serverChangeEvent = new object ();
45
46
47                 public HtmlInputText ()
48                         : base ("text")
49                 {
50                 }
51
52                 public HtmlInputText (string type)
53                         : base (type)
54                 {
55                 }
56
57
58                 [DefaultValue ("")]
59                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
60                 [WebSysDescription("")]
61                 [WebCategory("Behavior")]
62                 public int MaxLength {
63                         get {
64                                 string s = Attributes ["maxlength"];
65                                 return (s == null) ? -1 : Convert.ToInt32 (s);
66                         }
67                         set {
68                                 if (value == -1)
69                                         Attributes.Remove ("maxlength");
70                                 else
71                                         Attributes ["maxlength"] = value.ToString ();
72                         }
73                 }
74
75 #if NET_2_0
76                 [DefaultValue (-1)]
77 #else
78                 [DefaultValue ("")]
79 #endif
80                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
81                 [WebSysDescription("")]
82                 [WebCategory("Appearance")]
83                 public int Size {
84                         get {
85                                 string s = Attributes ["size"];
86                                 return (s == null) ? -1 : Convert.ToInt32 (s);
87                         }
88                         set {
89                                 if (value == -1)
90                                         Attributes.Remove ("size");
91                                 else
92                                         Attributes ["size"] = value.ToString ();
93                         }
94                 }
95
96                 public override string Value {
97                         get {
98                                 string s = Attributes ["value"];
99                                 return (s == null) ? String.Empty : s;
100                         }
101                         set {
102                                 if (value == null)
103                                         Attributes.Remove ("value");
104                                 else
105                                         Attributes ["value"] = value;
106                         }
107                 }
108
109
110 #if NET_2_0
111                 protected internal
112 #else           
113                 protected
114 #endif          
115                 override void OnPreRender (EventArgs e)
116                 {
117                         base.OnPreRender (e);
118                         if (Page != null) {
119                                 Page.RegisterRequiresPostBack (this);
120                         }
121                 }
122
123                 protected virtual void OnServerChange (EventArgs e)
124                 {
125                         EventHandler serverChange = (EventHandler) Events [serverChangeEvent];
126                         if (serverChange != null)
127                                 serverChange (this, e);
128                 }
129
130 #if !NET_2_0
131                 protected override void RenderAttributes (HtmlTextWriter writer)
132                 {
133                         // the Type property can be, indirectly, changed by using the Attributes property
134                         if (String.Compare (Type, 0, "password", 0, 8, true, CultureInfo.InvariantCulture) == 0) {
135                                 Attributes.Remove ("value");
136                         }
137
138                         base.RenderAttributes (writer);
139                 }
140 #endif
141
142                 bool LoadPostDataInternal (string postDataKey, NameValueCollection postCollection)
143                 {
144                         string s = postCollection [postDataKey];
145                         if (Value != s) {
146                                 Value = s;
147                                 return true;
148                         }
149                         return false;
150                 }
151
152                 void RaisePostDataChangedEventInternal ()
153                 {
154                         OnServerChange (EventArgs.Empty);
155                 }
156
157 #if NET_2_0
158                 protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
159                 {
160                         return LoadPostDataInternal (postDataKey, postCollection);
161                 }
162
163                 protected virtual void RaisePostDataChangedEvent ()
164                 {
165                         RaisePostDataChangedEventInternal ();
166                 }
167 #endif
168
169                 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
170                 {
171 #if NET_2_0
172                         return LoadPostData (postDataKey, postCollection);
173 #else
174                         return LoadPostDataInternal (postDataKey, postCollection);
175 #endif
176                 }
177
178                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
179                 {
180 #if NET_2_0
181                         RaisePostDataChangedEvent ();
182 #else
183                         RaisePostDataChangedEventInternal ();
184 #endif
185                 }
186
187
188                 [WebSysDescription("")]
189                 [WebCategory("Action")]
190                 public event EventHandler ServerChange {
191                         add { Events.AddHandler (serverChangeEvent, value); }
192                         remove { Events.RemoveHandler (serverChangeEvent, value); }
193                 }
194         }
195 }