2 // System.Web.UI.WebControls.TextBox.cs
5 // Gaurav Vaish (gvaish@iitk.ac.in)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 // (C) Gaurav Vaish (2002)
9 // (C) 2003 Andreas Nahr
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System
.Collections
.Specialized
;
35 using System
.ComponentModel
;
36 using System
.Globalization
;
40 namespace System
.Web
.UI
.WebControls
42 [ControlBuilder (typeof (TextBoxControlBuilder
))]
43 [DefaultEvent("TextChanged")]
44 [DefaultProperty("Text")]
45 [ParseChildren(false)]
46 [ValidationProperty("Text")]
47 [DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, " + Consts
.AssemblySystem_Design
)]
48 public class TextBox
: WebControl
, IPostBackDataHandler
50 static readonly object TextChangedEvent
= new object ();
52 public TextBox() : base (HtmlTextWriterTag
.Input
)
56 [DefaultValue (false), WebCategory ("Behavior")]
57 [WebSysDescription ("The control automatically posts back after changing the text.")]
58 public virtual bool AutoPostBack
{
60 object o
= ViewState
["AutoPostBack"];
61 return (o
== null) ? false : (bool) o
;
64 set { ViewState ["AutoPostBack"] = value; }
67 [DefaultValue (0), Bindable (true), WebCategory ("Appearance")]
68 [WebSysDescription ("The width of this control specified in characters.")]
69 public virtual int Columns
{
71 object o
= ViewState
["Columns"];
72 return (o
== null) ? 0 : (int) o
;
77 throw new ArgumentOutOfRangeException ("value",
78 "Columns value has to be 0 for 'not set' or bigger than 0.");
80 ViewState
["Columns"] = value;
84 [DefaultValue (0), Bindable (true), WebCategory ("Behavior")]
85 [WebSysDescription ("The maximum number of characters you can enter in this control.")]
86 public virtual int MaxLength
{
88 object o
= ViewState
["MaxLength"];
89 return (o
== null) ? 0 : (int) o
;
94 throw new ArgumentOutOfRangeException ("value",
95 "MaxLength value has to be 0 for 'not set' or bigger than 0.");
97 ViewState
["MaxLength"] = value;
101 [DefaultValue (false), Bindable (true), WebCategory ("Behavior")]
102 [WebSysDescription ("If the control is ReadOnly you cannot enter new text.")]
103 public virtual bool ReadOnly
{
105 object o
= ViewState
["ReadOnly"];
106 return (o
== null) ? false : (bool) o
;
108 set { ViewState ["ReadOnly"] = value; }
111 [DefaultValue (0), Bindable (true), WebCategory ("Behavior")]
112 [WebSysDescription ("The number of lines that this multiline contol spans.")]
113 public virtual int Rows
{
115 object o
= ViewState
["Rows"];
116 return (o
== null) ? 0 : (int) o
;
121 throw new ArgumentOutOfRangeException ("value",
122 "Rows value has to be 0 for 'not set' or bigger than 0.");
123 ViewState
["Rows"] = value;
127 [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
128 [PersistenceMode (PersistenceMode
.EncodedInnerDefaultProperty
)]
129 [WebSysDescription ("The text that this control initially displays.")]
130 public virtual string Text
{
132 object o
= ViewState
["Text"];
133 return (o
== null) ? String
.Empty
: (string) o
;
136 set { ViewState ["Text"] = value; }
139 [DefaultValue (typeof (TextBoxMode
), "SingleLine"), WebCategory ("Behavior")]
140 [WebSysDescription ("A mode of how the control operates.")]
141 public virtual TextBoxMode TextMode
{
143 object o
= ViewState
["TextMode"];
144 return (o
== null) ? TextBoxMode
.SingleLine
: (TextBoxMode
) o
;
148 if(!Enum
.IsDefined (typeof(TextBoxMode
), value))
149 throw new ArgumentOutOfRangeException ("value",
150 "Only existing modes are allowed");
152 ViewState
["TextMode"] = value;
156 [DefaultValue (true), WebCategory ("Layout")]
157 [WebSysDescription ("Determines if a line wraps at line-end.")]
158 public virtual bool Wrap
{
160 object o
= ViewState
["Wrap"];
161 return (o
== null) ? true : (bool) o
;
164 set { ViewState ["Wrap"] = value; }
168 [WebCategory ("Action")]
169 [WebSysDescription ("Raised when the text is changed.")]
170 public event EventHandler TextChanged
{
171 add { Events.AddHandler (TextChangedEvent, value); }
172 remove { Events.RemoveHandler (TextChangedEvent, value); }
175 protected override HtmlTextWriterTag TagKey
{
177 if(TextMode
== TextBoxMode
.MultiLine
)
178 return HtmlTextWriterTag
.Textarea
;
179 return HtmlTextWriterTag
.Input
;
183 protected override void AddAttributesToRender (HtmlTextWriter writer
)
186 Page
.VerifyRenderingInServerForm (this);
188 NumberFormatInfo invar
= NumberFormatInfo
.InvariantInfo
;
190 writer
.AddAttribute (HtmlTextWriterAttribute
.Name
, UniqueID
);
191 if (TextMode
== TextBoxMode
.MultiLine
) {
193 writer
.AddAttribute (HtmlTextWriterAttribute
.Rows
,
194 Rows
.ToString (invar
));
197 writer
.AddAttribute (HtmlTextWriterAttribute
.Cols
,
198 Columns
.ToString (invar
));
201 writer
.AddAttribute(HtmlTextWriterAttribute
.Wrap
, "off");
204 if (TextMode
== TextBoxMode
.Password
) {
209 writer
.AddAttribute (HtmlTextWriterAttribute
.Value
, Text
);
212 writer
.AddAttribute (HtmlTextWriterAttribute
.Type
, mode
);
214 writer
.AddAttribute (HtmlTextWriterAttribute
.Maxlength
,
215 MaxLength
.ToString (invar
));
218 writer
.AddAttribute (HtmlTextWriterAttribute
.Size
,
219 Columns
.ToString (invar
));
223 writer
.AddAttribute (HtmlTextWriterAttribute
.ReadOnly
, "readonly");
225 base.AddAttributesToRender (writer
);
227 if (AutoPostBack
&& Page
!= null){
228 writer
.AddAttribute (HtmlTextWriterAttribute
.Onchange
,
229 Page
.GetPostBackClientEvent (this, ""));
230 writer
.AddAttribute ("language", "javascript");
234 protected override void AddParsedSubObject(object obj
)
236 if(!(obj
is LiteralControl
))
237 throw new HttpException ("Cannot have children of type" + obj
.GetType ());
239 Text
= ((LiteralControl
) obj
).Text
;
242 protected override void OnPreRender (EventArgs e
)
244 base.OnPreRender (e
);
245 bool enabled
= Enabled
;
247 if (AutoPostBack
&& enabled
)
248 Page
.RequiresPostBackScript ();
251 /* Don't save passwords in ViewState */
252 if (TextMode
== TextBoxMode
.Password
||
253 (enabled
&& Visible
&& Events
[TextChangedEvent
] == null))
254 ViewState
.SetItemDirty ("Text", false);
257 protected virtual void OnTextChanged (EventArgs e
)
260 EventHandler eh
= (EventHandler
) (Events
[TextChangedEvent
]);
266 protected override void Render (HtmlTextWriter writer
)
268 RenderBeginTag(writer
);
269 if (TextMode
== TextBoxMode
.MultiLine
)
270 HttpUtility
.HtmlEncode (Text
, writer
);
271 RenderEndTag(writer
);
274 bool IPostBackDataHandler
.LoadPostData (string postDataKey
,
275 NameValueCollection postCollection
)
277 if (postCollection
[postDataKey
] != Text
){
278 Text
= postCollection
[postDataKey
];
284 void IPostBackDataHandler
.RaisePostDataChangedEvent ()
286 OnTextChanged (EventArgs
.Empty
);