2
0

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. [PersistChildren(false)]
  25. [ValidationProperty("Text")]
  26. public class TextBox : WebControl, IPostBackDataHandler
  27. {
  28. private static readonly object TextChangedEvent = new object ();
  29. public TextBox() : base (HtmlTextWriterTag.Input)
  30. {
  31. }
  32. public virtual bool AutoPostBack
  33. {
  34. get {
  35. object o = ViewState ["AutoPostBack"];
  36. return (o == null) ? false : (bool) o;
  37. }
  38. set { ViewState ["AutoPostBack"] = value; }
  39. }
  40. public virtual int Columns
  41. {
  42. get {
  43. object o = ViewState ["Columns"];
  44. return (o == null) ? 0 : (int) o;
  45. }
  46. set { ViewState ["Columns"] = value; }
  47. }
  48. public virtual int MaxLength
  49. {
  50. get
  51. {
  52. object o = ViewState ["MaxLength"];
  53. return (o == null) ? 0 : (int) o;
  54. }
  55. set { ViewState ["MaxLength"] = value; }
  56. }
  57. public virtual bool ReadOnly
  58. {
  59. get
  60. {
  61. object o = ViewState ["ReadOnly"];
  62. return (o == null) ? false : (bool) o;
  63. }
  64. set { ViewState ["ReadOnly"] = value; }
  65. }
  66. public virtual int Rows
  67. {
  68. get
  69. {
  70. object o = ViewState ["Rows"];
  71. return (o == null) ? 0 : (int) o;
  72. }
  73. set { ViewState ["Rows"] = value; }
  74. }
  75. public virtual string Text
  76. {
  77. get {
  78. object o = ViewState ["Text"];
  79. return (o == null) ? String.Empty : (string) o;
  80. }
  81. set { ViewState ["Text"] = value; }
  82. }
  83. public virtual TextBoxMode TextMode
  84. {
  85. get {
  86. object o = ViewState ["TextMode"];
  87. return (o == null) ? TextBoxMode.SingleLine : (TextBoxMode) o;
  88. }
  89. set {
  90. if(!Enum.IsDefined (typeof(TextBoxMode), value))
  91. throw new ArgumentException ();
  92. ViewState ["TextMode"] = value;
  93. }
  94. }
  95. public virtual bool Wrap
  96. {
  97. get {
  98. object o = ViewState ["Wrap"];
  99. return (o == null) ? false : (bool) o;
  100. }
  101. set { ViewState ["Wrap"] = value; }
  102. }
  103. public event EventHandler TextChanged
  104. {
  105. add { Events.AddHandler (TextChangedEvent, value); }
  106. remove { Events.RemoveHandler (TextChangedEvent, value); }
  107. }
  108. protected override HtmlTextWriterTag TagKey
  109. {
  110. get {
  111. if(TextMode == TextBoxMode.MultiLine)
  112. return HtmlTextWriterTag.Textarea;
  113. return HtmlTextWriterTag.Input;
  114. }
  115. }
  116. protected override void AddAttributesToRender (HtmlTextWriter writer)
  117. {
  118. if(Page != null)
  119. Page.VerifyRenderingInServerForm (this);
  120. writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
  121. if (TextMode == TextBoxMode.MultiLine){
  122. if (Rows > 0)
  123. writer.AddAttribute (HtmlTextWriterAttribute.Rows,
  124. Rows.ToString (
  125. NumberFormatInfo.InvariantInfo));
  126. if (Columns > 0)
  127. writer.AddAttribute (HtmlTextWriterAttribute.Cols,
  128. Columns.ToString (
  129. NumberFormatInfo.InvariantInfo));
  130. if (!Wrap)
  131. writer.AddAttribute(HtmlTextWriterAttribute.Wrap, "off");
  132. } else {
  133. string mode;
  134. if (TextMode == TextBoxMode.Password)
  135. mode = "password";
  136. else {
  137. mode = "text";
  138. if (Text.Length > 0)
  139. writer.AddAttribute (HtmlTextWriterAttribute.Value, Text);
  140. }
  141. writer.AddAttribute (HtmlTextWriterAttribute.Type, mode);
  142. if (MaxLength > 0)
  143. writer.AddAttribute (HtmlTextWriterAttribute.Maxlength,
  144. MaxLength.ToString (NumberFormatInfo.InvariantInfo));
  145. if (Columns > 0)
  146. writer.AddAttribute (HtmlTextWriterAttribute.Size,
  147. Columns.ToString (NumberFormatInfo.InvariantInfo));
  148. }
  149. if (ReadOnly)
  150. writer.AddAttribute (HtmlTextWriterAttribute.ReadOnly, "readonly");
  151. base.AddAttributesToRender (writer);
  152. if (AutoPostBack && Page != null){
  153. writer.AddAttribute (HtmlTextWriterAttribute.Onchange,
  154. Page.GetPostBackClientEvent (this, ""));
  155. writer.AddAttribute ("language", "javascript");
  156. }
  157. }
  158. protected override void AddParsedSubObject(object obj)
  159. {
  160. if(!(obj is LiteralControl))
  161. throw new HttpException (HttpRuntime.FormatResourceString (
  162. "Cannot_Have_Children_Of_Type", "TextBox",
  163. GetType ().Name.ToString ()));
  164. Text = ((LiteralControl) obj).Text;
  165. }
  166. protected override void OnPreRender (EventArgs e)
  167. {
  168. base.OnPreRender (e);
  169. //FIXME
  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. }