TextBox.cs 5.3 KB

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