Removed another batch of pre 2.0 code + some cosmetics
[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-2010 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 using System.Web.Util;
34
35 namespace System.Web.UI.HtmlControls
36 {
37         // CAS
38         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40         // attributes
41         [DefaultEvent ("ServerChange")]
42         [ValidationProperty ("Value")]
43         [SupportsEventValidation]
44         public class HtmlInputText : HtmlInputControl, IPostBackDataHandler 
45         {
46                 static readonly object serverChangeEvent = new object ();
47
48                 public HtmlInputText ()
49                         : base ("text")
50                 {
51                 }
52
53                 public HtmlInputText (string type)
54                         : base (type)
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                 [DefaultValue (-1)]
76                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
77                 [WebSysDescription("")]
78                 [WebCategory("Appearance")]
79                 public int Size {
80                         get {
81                                 string s = Attributes ["size"];
82                                 return (s == null) ? -1 : Convert.ToInt32 (s);
83                         }
84                         set {
85                                 if (value == -1)
86                                         Attributes.Remove ("size");
87                                 else
88                                         Attributes ["size"] = value.ToString ();
89                         }
90                 }
91
92                 public override string Value {
93                         get {
94                                 string s = Attributes ["value"];
95                                 return (s == null) ? String.Empty : s;
96                         }
97                         set {
98                                 if (value == null)
99                                         Attributes.Remove ("value");
100                                 else
101                                         Attributes ["value"] = value;
102                         }
103                 }
104
105                 protected internal override void Render (HtmlTextWriter writer)
106                 {
107                         Page page = Page;
108                         if (page != null)
109                                 page.ClientScript.RegisterForEventValidation (UniqueID);
110                         base.Render (writer);
111                 }
112
113                 protected internal override void OnPreRender (EventArgs e)
114                 {
115                         base.OnPreRender (e);
116
117                         Page page = Page;
118                         if (page != null && !Disabled) {
119                                 page.RegisterRequiresPostBack (this);
120                                 page.RegisterEnabledControl (this);
121                         }
122                 }
123
124                 protected virtual void OnServerChange (EventArgs e)
125                 {
126                         EventHandler serverChange = (EventHandler) Events [serverChangeEvent];
127                         if (serverChange != null)
128                                 serverChange (this, e);
129                 }
130
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, Helpers.InvariantCulture) == 0) {
135                                 Attributes.Remove ("value");
136                         }
137
138                         base.RenderAttributes (writer);
139                 }
140
141                 bool LoadPostDataInternal (string postDataKey, NameValueCollection postCollection)
142                 {
143                         string s = postCollection [postDataKey];
144                         if (Value != s) {
145                                 Value = s;
146                                 return true;
147                         }
148                         return false;
149                 }
150
151                 void RaisePostDataChangedEventInternal ()
152                 {
153                         OnServerChange (EventArgs.Empty);
154                 }
155
156                 protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
157                 {
158                         return LoadPostDataInternal (postDataKey, postCollection);
159                 }
160
161                 protected virtual void RaisePostDataChangedEvent ()
162                 {
163                         ValidateEvent (UniqueID, String.Empty);
164                         RaisePostDataChangedEventInternal ();
165                 }
166
167                 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
168                 {
169                         return LoadPostData (postDataKey, postCollection);
170                 }
171
172                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
173                 {
174                         RaisePostDataChangedEvent ();
175                 }
176
177                 [WebSysDescription("")]
178                 [WebCategory("Action")]
179                 public event EventHandler ServerChange {
180                         add { Events.AddHandler (serverChangeEvent, value); }
181                         remove { Events.RemoveHandler (serverChangeEvent, value); }
182                 }
183         }
184 }