2
0

TextBox.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. //
  2. // System.Web.UI.WebControls.TextBox.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Gaurav Vaish (2002)
  9. // (C) 2003 Andreas Nahr
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections.Specialized;
  33. using System.ComponentModel;
  34. using System.Globalization;
  35. using System.Web;
  36. using System.Web.UI;
  37. namespace System.Web.UI.WebControls
  38. {
  39. [ControlBuilder (typeof (TextBoxControlBuilder))]
  40. [DefaultEvent("TextChanged")]
  41. [DefaultProperty("Text")]
  42. [ParseChildren(false)]
  43. [ValidationProperty("Text")]
  44. [DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
  45. public class TextBox : WebControl, IPostBackDataHandler
  46. {
  47. static readonly object TextChangedEvent = new object ();
  48. public TextBox() : base (HtmlTextWriterTag.Input)
  49. {
  50. }
  51. [DefaultValue (false), WebCategory ("Behavior")]
  52. [WebSysDescription ("The control automatically posts back after changing the text.")]
  53. public virtual bool AutoPostBack {
  54. get {
  55. object o = ViewState ["AutoPostBack"];
  56. return (o == null) ? false : (bool) o;
  57. }
  58. set { ViewState ["AutoPostBack"] = value; }
  59. }
  60. [DefaultValue (0), Bindable (true), WebCategory ("Appearance")]
  61. [WebSysDescription ("The width of this control specified in characters.")]
  62. public virtual int Columns {
  63. get {
  64. object o = ViewState ["Columns"];
  65. return (o == null) ? 0 : (int) o;
  66. }
  67. set {
  68. if (value < 0)
  69. throw new ArgumentOutOfRangeException ("value",
  70. "Columns value has to be 0 for 'not set' or bigger than 0.");
  71. ViewState ["Columns"] = value;
  72. }
  73. }
  74. [DefaultValue (0), Bindable (true), WebCategory ("Behavior")]
  75. [WebSysDescription ("The maximum number of characters you can enter in this control.")]
  76. public virtual int MaxLength {
  77. get {
  78. object o = ViewState ["MaxLength"];
  79. return (o == null) ? 0 : (int) o;
  80. }
  81. set {
  82. if (value < 0)
  83. throw new ArgumentOutOfRangeException ("value",
  84. "MaxLength value has to be 0 for 'not set' or bigger than 0.");
  85. ViewState ["MaxLength"] = value;
  86. }
  87. }
  88. [DefaultValue (false), Bindable (true), WebCategory ("Behavior")]
  89. [WebSysDescription ("If the control is ReadOnly you cannot enter new text.")]
  90. public virtual bool ReadOnly {
  91. get {
  92. object o = ViewState ["ReadOnly"];
  93. return (o == null) ? false : (bool) o;
  94. }
  95. set { ViewState ["ReadOnly"] = value; }
  96. }
  97. [DefaultValue (0), Bindable (true), WebCategory ("Behavior")]
  98. [WebSysDescription ("The number of lines that this multiline contol spans.")]
  99. public virtual int Rows {
  100. get {
  101. object o = ViewState ["Rows"];
  102. return (o == null) ? 0 : (int) o;
  103. }
  104. set {
  105. if (value < 0)
  106. throw new ArgumentOutOfRangeException ("value",
  107. "Rows value has to be 0 for 'not set' or bigger than 0.");
  108. ViewState ["Rows"] = value;
  109. }
  110. }
  111. [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
  112. [PersistenceMode (PersistenceMode.EncodedInnerDefaultProperty)]
  113. [WebSysDescription ("The text that this control initially displays.")]
  114. public virtual string Text {
  115. get {
  116. object o = ViewState ["Text"];
  117. return (o == null) ? String.Empty : (string) o;
  118. }
  119. set { ViewState ["Text"] = value; }
  120. }
  121. [DefaultValue (typeof (TextBoxMode), "SingleLine"), WebCategory ("Behavior")]
  122. [WebSysDescription ("A mode of how the control operates.")]
  123. public virtual TextBoxMode TextMode {
  124. get {
  125. object o = ViewState ["TextMode"];
  126. return (o == null) ? TextBoxMode.SingleLine : (TextBoxMode) o;
  127. }
  128. set {
  129. if(!Enum.IsDefined (typeof(TextBoxMode), value))
  130. throw new ArgumentOutOfRangeException ("value",
  131. "Only existing modes are allowed");
  132. ViewState ["TextMode"] = value;
  133. }
  134. }
  135. [DefaultValue (true), WebCategory ("Layout")]
  136. [WebSysDescription ("Determines if a line wraps at line-end.")]
  137. public virtual bool Wrap {
  138. get {
  139. object o = ViewState ["Wrap"];
  140. return (o == null) ? true : (bool) o;
  141. }
  142. set { ViewState ["Wrap"] = value; }
  143. }
  144. [WebCategory ("Action")]
  145. [WebSysDescription ("Raised when the text is changed.")]
  146. public event EventHandler TextChanged {
  147. add { Events.AddHandler (TextChangedEvent, value); }
  148. remove { Events.RemoveHandler (TextChangedEvent, value); }
  149. }
  150. protected override HtmlTextWriterTag TagKey {
  151. get {
  152. if(TextMode == TextBoxMode.MultiLine)
  153. return HtmlTextWriterTag.Textarea;
  154. return HtmlTextWriterTag.Input;
  155. }
  156. }
  157. protected override void AddAttributesToRender (HtmlTextWriter writer)
  158. {
  159. if(Page != null)
  160. Page.VerifyRenderingInServerForm (this);
  161. NumberFormatInfo invar = NumberFormatInfo.InvariantInfo;
  162. writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
  163. if (TextMode == TextBoxMode.MultiLine) {
  164. if (Rows > 0)
  165. writer.AddAttribute (HtmlTextWriterAttribute.Rows,
  166. Rows.ToString (invar));
  167. if (Columns > 0)
  168. writer.AddAttribute (HtmlTextWriterAttribute.Cols,
  169. Columns.ToString (invar));
  170. if (!Wrap)
  171. writer.AddAttribute(HtmlTextWriterAttribute.Wrap, "off");
  172. } else {
  173. string mode;
  174. if (TextMode == TextBoxMode.Password) {
  175. mode = "password";
  176. } else {
  177. mode = "text";
  178. if (Text.Length > 0)
  179. writer.AddAttribute (HtmlTextWriterAttribute.Value, Text);
  180. }
  181. writer.AddAttribute (HtmlTextWriterAttribute.Type, mode);
  182. if (MaxLength > 0)
  183. writer.AddAttribute (HtmlTextWriterAttribute.Maxlength,
  184. MaxLength.ToString (invar));
  185. if (Columns > 0)
  186. writer.AddAttribute (HtmlTextWriterAttribute.Size,
  187. Columns.ToString (invar));
  188. }
  189. if (ReadOnly)
  190. writer.AddAttribute (HtmlTextWriterAttribute.ReadOnly, "readonly");
  191. base.AddAttributesToRender (writer);
  192. if (AutoPostBack && Page != null){
  193. writer.AddAttribute (HtmlTextWriterAttribute.Onchange,
  194. Page.GetPostBackClientEvent (this, ""));
  195. writer.AddAttribute ("language", "javascript");
  196. }
  197. }
  198. protected override void AddParsedSubObject(object obj)
  199. {
  200. if(!(obj is LiteralControl))
  201. throw new HttpException ("Cannot have children of type" + obj.GetType ());
  202. Text = ((LiteralControl) obj).Text;
  203. }
  204. protected override void OnPreRender (EventArgs e)
  205. {
  206. base.OnPreRender (e);
  207. bool enabled = Enabled;
  208. if (Page != null) {
  209. if (AutoPostBack && enabled)
  210. Page.RequiresPostBackScript ();
  211. }
  212. /* Don't save passwords in ViewState */
  213. if (TextMode == TextBoxMode.Password ||
  214. (enabled && Visible && Events [TextChangedEvent] == null))
  215. ViewState.SetItemDirty ("Text", false);
  216. }
  217. protected virtual void OnTextChanged (EventArgs e)
  218. {
  219. if(Events != null){
  220. EventHandler eh = (EventHandler) (Events [TextChangedEvent]);
  221. if(eh != null)
  222. eh (this, e);
  223. }
  224. }
  225. protected override void Render (HtmlTextWriter writer)
  226. {
  227. RenderBeginTag(writer);
  228. if (TextMode == TextBoxMode.MultiLine)
  229. HttpUtility.HtmlEncode (Text, writer);
  230. RenderEndTag(writer);
  231. }
  232. bool IPostBackDataHandler.LoadPostData (string postDataKey,
  233. NameValueCollection postCollection)
  234. {
  235. if (postCollection [postDataKey] != Text){
  236. Text = postCollection [postDataKey];
  237. return true;
  238. }
  239. return false;
  240. }
  241. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  242. {
  243. OnTextChanged (EventArgs.Empty);
  244. }
  245. }
  246. }