1 //------------------------------------------------------------------------------
2 // <copyright file="HtmlTextArea.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6 
7 /*
8  * HtmlTextArea.cs
9  *
10  * Copyright (c) 2000 Microsoft Corporation
11  */
12 
13 namespace System.Web.UI.HtmlControls {
14     using System.ComponentModel;
15     using System;
16     using System.Collections;
17     using System.Collections.Specialized;
18     using System.Web;
19     using System.Web.UI;
20     using System.Globalization;
21     using System.Security.Permissions;
22 
23 
24     /// <devdoc>
25     ///    <para>Defines the methods, properties, and events for the
26     ///    <see cref='System.Web.UI.HtmlControls.HtmlTextArea'/>
27     ///    class that
28     ///    allows programmatic access to the HTML &lt;textarea&gt;.</para>
29     /// </devdoc>
30     [
31     DefaultEvent("ServerChange"),
32     SupportsEventValidation,
33     ValidationProperty("Value")
34     ]
35     public class HtmlTextArea : HtmlContainerControl, IPostBackDataHandler {
36 
37         private static readonly object EventServerChange = new object();
38 
39         /*
40          *  Creates an intrinsic Html TEXTAREA control.
41          */
42 
43         /// <devdoc>
44         ///    Initializes a new instance of the <see cref='System.Web.UI.HtmlControls.HtmlTextArea'/> class.
45         /// </devdoc>
HtmlTextArea()46         public HtmlTextArea() : base("textarea") {
47         }
48 
49         /*
50          * The property for the number of columns to display.
51          */
52 
53         /// <devdoc>
54         ///    <para> Indicates the display width (in characters) of the
55         ///       text area.</para>
56         /// </devdoc>
57         [
58         WebCategory("Appearance"),
59         DefaultValue(""),
60         DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
61         ]
62         public int Cols {
63             get {
64                 string s = Attributes["cols"];
65                 return((s != null) ? Int32.Parse(s, CultureInfo.InvariantCulture) : -1);
66             }
67             set {
68                 Attributes["cols"] = MapIntegerAttributeToString(value);
69             }
70         }
71 
72         /*
73          * Name property.
74          */
75 
76         /// <devdoc>
77         ///    <para>
78         ///       Gets the value of the HTML
79         ///       Name attribute that will be rendered to the browser.
80         ///    </para>
81         /// </devdoc>
82         [
83         WebCategory("Behavior"),
84         DefaultValue(""),
85         DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
86         ]
87         public virtual string Name {
88             get {
89                 return UniqueID;
90                 //string s = Attributes["name"];
91                 //return ((s != null) ? s : "");
92             }
93             set {
94                 //Attributes["name"] = MapStringAttributeToString(value);
95             }
96         }
97 
98         // Value that gets rendered for the Name attribute
99         internal string RenderedNameAttribute {
100             get {
101                 return Name;
102                 //string name = Name;
103                 //if (name.Length == 0)
104                 //    return UniqueID;
105 
106                 //return name;
107             }
108         }
109 
110         /*
111          * The property for the number of rows to display.
112          */
113 
114         /// <devdoc>
115         ///    <para>Gets or sets the display height (in rows) of the text area.</para>
116         /// </devdoc>
117         [
118         WebCategory("Appearance"),
119         DefaultValue(""),
120         DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
121         ]
122         public int Rows {
123             get {
124                 string s = Attributes["rows"];
125                 return((s != null) ? Int32.Parse(s, CultureInfo.InvariantCulture) : -1);
126             }
127             set {
128                 Attributes["rows"] = MapIntegerAttributeToString(value);
129             }
130         }
131 
132 
133         /// <devdoc>
134         ///    <para>Gets or sets the content of the text area.</para>
135         /// </devdoc>
136         [
137         WebCategory("Appearance"),
138         DefaultValue(""),
139         DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
140         ]
141         public string Value {
142             get {
143                 return InnerText;
144             }
145             set {
146                 InnerText = value;
147             }
148         }
149 
150 
151         /// <devdoc>
152         /// <para>Occurs when the content of the <see langword='HtmlTextArea'/> control is changed upon server
153         ///    postback.</para>
154         /// </devdoc>
155         [
156         WebCategory("Action"),
157         WebSysDescription(SR.HtmlTextArea_OnServerChange)
158         ]
159         public event EventHandler ServerChange {
160             add {
161                 Events.AddHandler(EventServerChange, value);
162             }
163             remove {
164                 Events.RemoveHandler(EventServerChange, value);
165             }
166         }
167 
168 
169         /// <internalonly/>
170         /// <devdoc>
171         ///    Overridden to only allow literal controls to be added as Text property.
172         /// </devdoc>
AddParsedSubObject(object obj)173         protected override void AddParsedSubObject(object obj) {
174             if (obj is LiteralControl || obj is DataBoundLiteralControl)
175                 base.AddParsedSubObject(obj);
176             else
177                 throw new HttpException(SR.GetString(SR.Cannot_Have_Children_Of_Type, "HtmlTextArea", obj.GetType().Name.ToString(CultureInfo.InvariantCulture)));
178         }
179 
180 
181         /// <internalonly/>
182         /// <devdoc>
183         /// </devdoc>
RenderAttributes(HtmlTextWriter writer)184         protected override void RenderAttributes(HtmlTextWriter writer) {
185             if (Page != null) {
186                 Page.ClientScript.RegisterForEventValidation(RenderedNameAttribute);
187             }
188 
189             writer.WriteAttribute("name", RenderedNameAttribute);
190             Attributes.Remove("name");
191 
192             base.RenderAttributes(writer);
193         }
194 
195 
196         /// <devdoc>
197         /// <para>Raised the <see langword='ServerChange'/>
198         /// event.</para>
199         /// </devdoc>
OnServerChange(EventArgs e)200         protected virtual void OnServerChange(EventArgs e) {
201             EventHandler handler = (EventHandler)Events[EventServerChange];
202             if (handler != null) handler(this, e);
203         }
204 
205 
206         /// <internalonly/>
207         /// <devdoc>
208         /// </devdoc>
OnPreRender(EventArgs e)209         protected internal override void OnPreRender(EventArgs e) {
210             base.OnPreRender(e);
211 
212             if (!Disabled) {
213                 // if no change handler, no need to save posted property
214                 if (Events[EventServerChange] == null) {
215                     ViewState.SetItemDirty("value",false);
216                 }
217 
218                 if (Page != null) {
219                     Page.RegisterEnabledControl(this);
220                 }
221             }
222         }
223 
224         /*
225          * Method of IPostBackDataHandler interface to process posted data.
226          * TextArea process a newly posted value.
227          */
228 
229         /// <internalonly/>
230         /// <devdoc>
231         /// </devdoc>
IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)232         bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection) {
233             return LoadPostData(postDataKey, postCollection);
234         }
235 
236 
LoadPostData(string postDataKey, NameValueCollection postCollection)237         protected virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection) {
238             string current = Value;
239             string text = postCollection.GetValues(postDataKey)[0];
240 
241             if (current == null || !current.Equals(text)) {
242                 ValidateEvent(postDataKey);
243 
244                 Value = text;
245                 return true;
246             }
247 
248             return false;
249         }
250 
251         /*
252          * Method of IPostBackDataHandler interface which is invoked whenever posted data
253          * for a control has changed.  TextArea fires an OnServerChange event.
254          */
255 
256         /// <internalonly/>
257         /// <devdoc>
258         /// </devdoc>
IPostBackDataHandler.RaisePostDataChangedEvent()259         void IPostBackDataHandler.RaisePostDataChangedEvent() {
260             RaisePostDataChangedEvent();
261         }
262 
263 
264         /// <internalonly/>
265         /// <devdoc>
266         /// </devdoc>
RaisePostDataChangedEvent()267         protected virtual void RaisePostDataChangedEvent() {
268             OnServerChange(EventArgs.Empty);
269         }
270     }
271 }
272