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