2008-11-18 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.HtmlControls / HtmlTextArea.cs
1 //
2 // System.Web.UI.HtmlControls.HtmlTextArea.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 HtmlTextArea : HtmlContainerControl, IPostBackDataHandler 
46         {
47                 static readonly object serverChangeEvent = new object ();
48
49                 public HtmlTextArea ()
50                         : base ("textarea")
51                 {
52                 }
53
54
55                 [DefaultValue ("")]
56                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
57                 [WebSysDescription("")]
58                 [WebCategory("Appearance")]
59                 public int Cols {
60                         get {
61                                 string s = Attributes ["cols"];
62                                 return (s == null) ? -1 : Convert.ToInt32 (s);
63                         }
64                         set {
65                                 if (value == -1)
66                                         Attributes.Remove ("cols");
67                                 else
68                                         Attributes ["cols"] = value.ToString (CultureInfo.InvariantCulture);
69                         }
70                 }
71
72                 [DefaultValue ("")]
73                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
74                 [WebSysDescription("")]
75                 [WebCategory("Behavior")]
76                 public virtual string Name {
77                         get { return UniqueID; }
78                         set { ; }
79                 }
80
81                 [DefaultValue ("")]
82                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
83                 [WebSysDescription("")]
84                 [WebCategory("Appearance")]
85                 public int Rows {
86                         get {
87                                 string s = Attributes ["rows"];
88                                 return (s == null) ? -1 : Convert.ToInt32 (s);
89                         }
90                         set {
91                                 if (value == -1)
92                                         Attributes.Remove ("rows");
93                                 else
94                                         Attributes ["rows"] = value.ToString (CultureInfo.InvariantCulture);
95                         }
96                 }
97
98                 [DefaultValue ("")]
99                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
100                 [WebSysDescription("")]
101                 [WebCategory("Appearance")]
102                 public string Value {
103                         get { return InnerText; }
104                         set { InnerText = value; }
105                 }
106
107
108                 protected override void AddParsedSubObject (object obj)
109                 {
110                         if (!((obj is LiteralControl) || (obj is DataBoundLiteralControl))) {
111                                 throw new HttpException (Locale.GetText ("Wrong type."));
112                         }
113                         base.AddParsedSubObject (obj);
114                 }
115
116 #if NET_2_0
117                 protected internal
118 #else           
119                 protected
120 #endif          
121                 override void OnPreRender (EventArgs e)
122                 {
123                         base.OnPreRender (e);
124
125                         if (Page != null && !Disabled) {
126                                 Page.RegisterRequiresPostBack (this);
127 #if NET_2_0
128                                 Page.RegisterEnabledControl (this);
129 #endif
130                         }
131                 }
132
133                 protected virtual void OnServerChange (EventArgs e)
134                 {
135                         EventHandler serverChange = (EventHandler) Events [serverChangeEvent];
136                         if (serverChange != null)
137                                 serverChange (this, e);
138                 }
139
140                 protected override void RenderAttributes (HtmlTextWriter writer)
141                 {
142 #if NET_2_0
143                         if (Page != null)
144                                 Page.ClientScript.RegisterForEventValidation (UniqueID);
145 #endif
146                         if (Attributes ["name"] == null) {
147                                 writer.WriteAttribute ("name", Name);
148                         }
149                         base.RenderAttributes (writer);
150                 }
151
152 #if NET_2_0
153                 protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
154                 {
155                         return DefaultLoadPostData (postDataKey, postCollection);
156                 }
157
158                 protected virtual void RaisePostDataChangedEvent ()
159                 {
160                         ValidateEvent (UniqueID, String.Empty);
161                         OnServerChange (EventArgs.Empty);
162                 }
163 #endif
164
165                 internal bool DefaultLoadPostData (string postDataKey, NameValueCollection postCollection)
166                 {
167                         string s = postCollection [postDataKey];
168                         if ((s != null) && (s != Value)) {
169                                 Value = s;
170                                 return true;
171                         }
172                         return false;
173                 }
174
175                 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
176                 {
177 #if NET_2_0
178                         return LoadPostData (postDataKey, postCollection);
179 #else
180                         return DefaultLoadPostData (postDataKey, postCollection);
181 #endif
182                 }
183
184                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
185                 {
186 #if NET_2_0
187                         RaisePostDataChangedEvent ();
188 #else
189                         OnServerChange (EventArgs.Empty);
190 #endif
191                 }
192
193
194                 [WebSysDescription("")]
195                 [WebCategory("Action")]
196                 public event EventHandler ServerChange {
197                         add { Events.AddHandler (serverChangeEvent, value); }
198                         remove { Events.RemoveHandler (serverChangeEvent, value); }
199                 }
200         }
201 }