TextBox.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: TextBox
  4. *
  5. * Author: Gaurav Vaish
  6. * Maintainer: [email protected]
  7. * Contact: <[email protected]>, <[email protected]>
  8. * Implementation: yes
  9. * Status: 80%
  10. *
  11. * (C) Gaurav Vaish (2002)
  12. */
  13. using System;
  14. using System.Collections.Specialized;
  15. using System.ComponentModel;
  16. using System.Globalization;
  17. using System.Web;
  18. using System.Web.UI;
  19. namespace System.Web.UI.WebControls
  20. {
  21. [DefaultEvent("TextChanged")]
  22. [DefaultProperty("Text")]
  23. [ParseChildren(false)]
  24. [ValidationProperty("Text")]
  25. public class TextBox : WebControl, IPostBackDataHandler
  26. {
  27. private static readonly object TextChangedEvent = new object ();
  28. public TextBox() : base (HtmlTextWriterTag.Input)
  29. {
  30. }
  31. public virtual bool AutoPostBack
  32. {
  33. get {
  34. object o = ViewState ["AutoPostBack"];
  35. return (o == null) ? false : (bool) o;
  36. }
  37. set { ViewState ["AutoPostBack"] = value; }
  38. }
  39. public virtual int Columns
  40. {
  41. get {
  42. object o = ViewState ["Columns"];
  43. return (o == null) ? 0 : (int) o;
  44. }
  45. set { ViewState ["Columns"] = value; }
  46. }
  47. public virtual int MaxLength
  48. {
  49. get
  50. {
  51. object o = ViewState ["MaxLength"];
  52. return (o == null) ? 0 : (int) o;
  53. }
  54. set { ViewState ["MaxLength"] = value; }
  55. }
  56. public virtual bool ReadOnly
  57. {
  58. get
  59. {
  60. object o = ViewState ["ReadOnly"];
  61. return (o == null) ? false : (bool) o;
  62. }
  63. set { ViewState ["ReadOnly"] = value; }
  64. }
  65. public virtual int Rows
  66. {
  67. get
  68. {
  69. object o = ViewState ["Rows"];
  70. return (o == null) ? 0 : (int) o;
  71. }
  72. set { ViewState ["Rows"] = value; }
  73. }
  74. public virtual string Text
  75. {
  76. get {
  77. object o = ViewState ["Text"];
  78. return (o == null) ? String.Empty : (string) o;
  79. }
  80. set { ViewState ["Text"] = value; }
  81. }
  82. public virtual TextBoxMode TextMode
  83. {
  84. get {
  85. object o = ViewState ["TextMode"];
  86. return (o == null) ? TextBoxMode.SingleLine : (TextBoxMode) o;
  87. }
  88. set {
  89. if(!Enum.IsDefined (typeof(TextBoxMode), value))
  90. throw new ArgumentException ();
  91. ViewState ["TextMode"] = value;
  92. }
  93. }
  94. public virtual bool Wrap
  95. {
  96. get {
  97. object o = ViewState ["Wrap"];
  98. return (o == null) ? true : (bool) o;
  99. }
  100. set { ViewState ["Wrap"] = value; }
  101. }
  102. public event EventHandler TextChanged
  103. {
  104. add { Events.AddHandler (TextChangedEvent, value); }
  105. remove { Events.RemoveHandler (TextChangedEvent, value); }
  106. }
  107. protected override HtmlTextWriterTag TagKey
  108. {
  109. get {
  110. if(TextMode == TextBoxMode.MultiLine)
  111. return HtmlTextWriterTag.Textarea;
  112. return HtmlTextWriterTag.Input;
  113. }
  114. }
  115. protected override void AddAttributesToRender (HtmlTextWriter writer)
  116. {
  117. if(Page != null)
  118. Page.VerifyRenderingInServerForm (this);
  119. writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
  120. if (TextMode == TextBoxMode.MultiLine){
  121. if (Rows > 0)
  122. writer.AddAttribute (HtmlTextWriterAttribute.Rows,
  123. Rows.ToString (
  124. NumberFormatInfo.InvariantInfo));
  125. if (Columns > 0)
  126. writer.AddAttribute (HtmlTextWriterAttribute.Cols,
  127. Columns.ToString (
  128. NumberFormatInfo.InvariantInfo));
  129. if (!Wrap)
  130. writer.AddAttribute(HtmlTextWriterAttribute.Wrap, "off");
  131. } else {
  132. string mode;
  133. if (TextMode == TextBoxMode.Password)
  134. mode = "password";
  135. else {
  136. mode = "text";
  137. if (Text.Length > 0)
  138. writer.AddAttribute (HtmlTextWriterAttribute.Value, Text);
  139. }
  140. writer.AddAttribute (HtmlTextWriterAttribute.Type, mode);
  141. if (MaxLength > 0)
  142. writer.AddAttribute (HtmlTextWriterAttribute.Maxlength,
  143. MaxLength.ToString (NumberFormatInfo.InvariantInfo));
  144. if (Columns > 0)
  145. writer.AddAttribute (HtmlTextWriterAttribute.Size,
  146. Columns.ToString (NumberFormatInfo.InvariantInfo));
  147. }
  148. if (ReadOnly)
  149. writer.AddAttribute (HtmlTextWriterAttribute.ReadOnly, "readonly");
  150. base.AddAttributesToRender (writer);
  151. if (AutoPostBack && Page != null){
  152. writer.AddAttribute (HtmlTextWriterAttribute.Onchange,
  153. Page.GetPostBackClientEvent (this, ""));
  154. writer.AddAttribute ("language", "javascript");
  155. }
  156. }
  157. protected override void AddParsedSubObject(object obj)
  158. {
  159. if(!(obj is LiteralControl))
  160. throw new HttpException (HttpRuntime.FormatResourceString (
  161. "Cannot_Have_Children_Of_Type", "TextBox",
  162. GetType ().Name.ToString ()));
  163. Text = ((LiteralControl) obj).Text;
  164. }
  165. protected override void OnPreRender (EventArgs e)
  166. {
  167. base.OnPreRender (e);
  168. if (Events [TextChangedEvent] == null)
  169. ViewState.SetItemDirty ("Text", false);
  170. }
  171. protected virtual void OnTextChanged (EventArgs e)
  172. {
  173. if(Events != null){
  174. EventHandler eh = (EventHandler) (Events [TextChangedEvent]);
  175. if(eh != null)
  176. eh (this, e);
  177. }
  178. }
  179. protected override void Render (HtmlTextWriter writer)
  180. {
  181. RenderBeginTag(writer);
  182. if (TextMode == TextBoxMode.MultiLine)
  183. HttpUtility.HtmlEncode (Text, writer);
  184. RenderEndTag(writer);
  185. }
  186. bool IPostBackDataHandler.LoadPostData (string postDataKey,
  187. NameValueCollection postCollection)
  188. {
  189. if (postCollection [postDataKey] != Text){
  190. Text = postCollection [postDataKey];
  191. return true;
  192. }
  193. return false;
  194. }
  195. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  196. {
  197. OnTextChanged (EventArgs.Empty);
  198. }
  199. }
  200. }