TextBox.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. using System;
  12. using System.Collections.Specialized;
  13. using System.ComponentModel;
  14. using System.Globalization;
  15. using System.Web;
  16. using System.Web.UI;
  17. namespace System.Web.UI.WebControls
  18. {
  19. [ControlBuilder (typeof (TextBoxControlBuilder))]
  20. [DefaultEvent("TextChanged")]
  21. [DefaultProperty("Text")]
  22. [ParseChildren(false)]
  23. [ValidationProperty("Text")]
  24. [DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
  25. public class TextBox : WebControl, IPostBackDataHandler
  26. {
  27. private static readonly object TextChangedEvent = new object ();
  28. public TextBox() : base (HtmlTextWriterTag.Input)
  29. {
  30. }
  31. [DefaultValue (false), WebCategory ("Behavior")]
  32. [WebSysDescription ("The control automatically posts back after changing the text.")]
  33. public virtual bool AutoPostBack
  34. {
  35. get {
  36. object o = ViewState ["AutoPostBack"];
  37. return (o == null) ? false : (bool) o;
  38. }
  39. set { ViewState ["AutoPostBack"] = value; }
  40. }
  41. [DefaultValue (0), Bindable (true), WebCategory ("Appearance")]
  42. [WebSysDescription ("The width of this control specified in characters.")]
  43. public virtual int Columns
  44. {
  45. get {
  46. object o = ViewState ["Columns"];
  47. return (o == null) ? 0 : (int) o;
  48. }
  49. set {
  50. if (value < 0)
  51. throw new ArgumentOutOfRangeException ("value", "Columns value has to be 0 for 'not set' or bigger than 0.");
  52. ViewState ["Columns"] = value;
  53. }
  54. }
  55. [DefaultValue (0), Bindable (true), WebCategory ("Behavior")]
  56. [WebSysDescription ("The maximum number of characters you can enter in this control.")]
  57. public virtual int MaxLength
  58. {
  59. get
  60. {
  61. object o = ViewState ["MaxLength"];
  62. return (o == null) ? 0 : (int) o;
  63. }
  64. set {
  65. if (value < 0)
  66. throw new ArgumentOutOfRangeException ("value", "MaxLength value has to be 0 for 'not set' or bigger than 0.");
  67. ViewState ["MaxLength"] = value;
  68. }
  69. }
  70. [DefaultValue (false), Bindable (true), WebCategory ("Behavior")]
  71. [WebSysDescription ("If the control is ReadOnly you cannot enter new text.")]
  72. public virtual bool ReadOnly
  73. {
  74. get
  75. {
  76. object o = ViewState ["ReadOnly"];
  77. return (o == null) ? false : (bool) o;
  78. }
  79. set { ViewState ["ReadOnly"] = value; }
  80. }
  81. [DefaultValue (0), Bindable (true), WebCategory ("Behavior")]
  82. [WebSysDescription ("The number of lines that this multiline contol spans.")]
  83. public virtual int Rows
  84. {
  85. get
  86. {
  87. object o = ViewState ["Rows"];
  88. return (o == null) ? 0 : (int) o;
  89. }
  90. set {
  91. if (value < 0)
  92. throw new ArgumentOutOfRangeException ("value", "Rows value has to be 0 for 'not set' or bigger than 0.");
  93. ViewState ["Rows"] = value;
  94. }
  95. }
  96. [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
  97. [PersistenceMode (PersistenceMode.EncodedInnerDefaultProperty)]
  98. [WebSysDescription ("The text that this control initially displays.")]
  99. public virtual string Text
  100. {
  101. get {
  102. object o = ViewState ["Text"];
  103. return (o == null) ? String.Empty : (string) o;
  104. }
  105. set { ViewState ["Text"] = value; }
  106. }
  107. [DefaultValue (typeof (TextBoxMode), "SingleLine"), WebCategory ("Behavior")]
  108. [WebSysDescription ("A mode of how the control operates.")]
  109. public virtual TextBoxMode TextMode
  110. {
  111. get {
  112. object o = ViewState ["TextMode"];
  113. return (o == null) ? TextBoxMode.SingleLine : (TextBoxMode) o;
  114. }
  115. set {
  116. if(!Enum.IsDefined (typeof(TextBoxMode), value))
  117. throw new ArgumentOutOfRangeException ("value", "Only existing modes are allowed");
  118. ViewState ["TextMode"] = value;
  119. }
  120. }
  121. [DefaultValue (true), WebCategory ("Layout")]
  122. [WebSysDescription ("Determines if a line wraps at line-end.")]
  123. public virtual bool Wrap
  124. {
  125. get {
  126. object o = ViewState ["Wrap"];
  127. return (o == null) ? true : (bool) o;
  128. }
  129. set { ViewState ["Wrap"] = value; }
  130. }
  131. [WebCategory ("Action")]
  132. [WebSysDescription ("Raised when the text is changed.")]
  133. public event EventHandler TextChanged
  134. {
  135. add { Events.AddHandler (TextChangedEvent, value); }
  136. remove { Events.RemoveHandler (TextChangedEvent, value); }
  137. }
  138. protected override HtmlTextWriterTag TagKey
  139. {
  140. get {
  141. if(TextMode == TextBoxMode.MultiLine)
  142. return HtmlTextWriterTag.Textarea;
  143. return HtmlTextWriterTag.Input;
  144. }
  145. }
  146. protected override void AddAttributesToRender (HtmlTextWriter writer)
  147. {
  148. if(Page != null)
  149. Page.VerifyRenderingInServerForm (this);
  150. writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
  151. if (TextMode == TextBoxMode.MultiLine){
  152. if (Rows > 0)
  153. writer.AddAttribute (HtmlTextWriterAttribute.Rows,
  154. Rows.ToString (
  155. NumberFormatInfo.InvariantInfo));
  156. if (Columns > 0)
  157. writer.AddAttribute (HtmlTextWriterAttribute.Cols,
  158. Columns.ToString (
  159. NumberFormatInfo.InvariantInfo));
  160. if (!Wrap)
  161. writer.AddAttribute(HtmlTextWriterAttribute.Wrap, "off");
  162. } else {
  163. string mode;
  164. if (TextMode == TextBoxMode.Password)
  165. mode = "password";
  166. else {
  167. mode = "text";
  168. if (Text.Length > 0)
  169. writer.AddAttribute (HtmlTextWriterAttribute.Value, Text);
  170. }
  171. writer.AddAttribute (HtmlTextWriterAttribute.Type, mode);
  172. if (MaxLength > 0)
  173. writer.AddAttribute (HtmlTextWriterAttribute.Maxlength,
  174. MaxLength.ToString (NumberFormatInfo.InvariantInfo));
  175. if (Columns > 0)
  176. writer.AddAttribute (HtmlTextWriterAttribute.Size,
  177. Columns.ToString (NumberFormatInfo.InvariantInfo));
  178. }
  179. if (ReadOnly)
  180. writer.AddAttribute (HtmlTextWriterAttribute.ReadOnly, "readonly");
  181. base.AddAttributesToRender (writer);
  182. if (AutoPostBack && Page != null){
  183. writer.AddAttribute (HtmlTextWriterAttribute.Onchange,
  184. Page.GetPostBackClientEvent (this, ""));
  185. writer.AddAttribute ("language", "javascript");
  186. }
  187. }
  188. protected override void AddParsedSubObject(object obj)
  189. {
  190. if(!(obj is LiteralControl))
  191. throw new HttpException (HttpRuntime.FormatResourceString (
  192. "Cannot_Have_Children_Of_Type", "TextBox",
  193. GetType ().Name.ToString ()));
  194. Text = ((LiteralControl) obj).Text;
  195. }
  196. protected override void OnPreRender (EventArgs e)
  197. {
  198. base.OnPreRender (e);
  199. if (Events [TextChangedEvent] == null)
  200. ViewState.SetItemDirty ("Text", false);
  201. }
  202. protected virtual void OnTextChanged (EventArgs e)
  203. {
  204. if(Events != null){
  205. EventHandler eh = (EventHandler) (Events [TextChangedEvent]);
  206. if(eh != null)
  207. eh (this, e);
  208. }
  209. }
  210. protected override void Render (HtmlTextWriter writer)
  211. {
  212. RenderBeginTag(writer);
  213. if (TextMode == TextBoxMode.MultiLine)
  214. HttpUtility.HtmlEncode (Text, writer);
  215. RenderEndTag(writer);
  216. }
  217. bool IPostBackDataHandler.LoadPostData (string postDataKey,
  218. NameValueCollection postCollection)
  219. {
  220. if (postCollection [postDataKey] != Text){
  221. Text = postCollection [postDataKey];
  222. return true;
  223. }
  224. return false;
  225. }
  226. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  227. {
  228. OnTextChanged (EventArgs.Empty);
  229. }
  230. }
  231. }