TextBox.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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.Web;
  16. using System.Web.UI;
  17. namespace System.Web.UI.WebControls
  18. {
  19. public class TextBox : WebControl, IPostBackDataHandler
  20. {
  21. private static readonly object TextChangedEvent = new object();
  22. public TextBox(): base(HtmlTextWriterTag.Input)
  23. {
  24. }
  25. public virtual bool AutoPostBack
  26. {
  27. get
  28. {
  29. object o = ViewState["AutoPostBack"];
  30. if(o!=null)
  31. return (bool)o;
  32. return false;
  33. }
  34. set
  35. {
  36. ViewState["AutoPostBack"] = value;
  37. }
  38. }
  39. public virtual int Columns
  40. {
  41. get
  42. {
  43. object o = ViewState["Columns"];
  44. if(o != null)
  45. return (int)o;
  46. return 0;
  47. }
  48. set
  49. {
  50. ViewState["Columns"] = value;
  51. }
  52. }
  53. public virtual int MaxLength
  54. {
  55. get
  56. {
  57. object o = ViewState["MaxLrngth"];
  58. if(o != null)
  59. return (int)o;
  60. return 0;
  61. }
  62. set
  63. {
  64. ViewState["MaxLrngth"] = value;
  65. }
  66. }
  67. public virtual bool ReadOnly
  68. {
  69. get
  70. {
  71. object o = ViewState["ReadOnly"];
  72. if(o != null)
  73. return (bool)o;
  74. return false;
  75. }
  76. set
  77. {
  78. ViewState["ReadOnly"] = value;
  79. }
  80. }
  81. public virtual int Rows
  82. {
  83. get
  84. {
  85. object o = ViewState["Rows"];
  86. if(o != null)
  87. return (int)o;
  88. return 0;
  89. }
  90. set
  91. {
  92. ViewState["Rows"] = value;
  93. }
  94. }
  95. public virtual string Text
  96. {
  97. get
  98. {
  99. object o = ViewState["Text"];
  100. if(o != null)
  101. return (string)o;
  102. return String.Empty;
  103. }
  104. set
  105. {
  106. ViewState["Text"] = value;
  107. }
  108. }
  109. public virtual TextBoxMode TextMode
  110. {
  111. get
  112. {
  113. object o = ViewState["TextMode"];
  114. if(o != null)
  115. return (TextBoxMode)o;
  116. return TextBoxMode.SingleLine;
  117. }
  118. set
  119. {
  120. if(!Enum.IsDefined(typeof(TextBoxMode), value))
  121. {
  122. throw new ArgumentException();
  123. }
  124. ViewState["TextMode"] = value;
  125. }
  126. }
  127. public virtual bool Wrap
  128. {
  129. get
  130. {
  131. object o = ViewState["Wrap"];
  132. if(o != null)
  133. return (bool)o;
  134. return false;
  135. }
  136. set
  137. {
  138. ViewState["Wrap"] = value;
  139. }
  140. }
  141. public event EventHandler TextChanged
  142. {
  143. add
  144. {
  145. Events.AddHandler(TextChangedEvent, value);
  146. }
  147. remove
  148. {
  149. Events.RemoveHandler(TextChangedEvent, value);
  150. }
  151. }
  152. protected override HtmlTextWriterTag TagKey
  153. {
  154. get
  155. {
  156. if(TextMode == TextBoxMode.MultiLine)
  157. {
  158. return HtmlTextWriterTag.Textarea;
  159. }
  160. return HtmlTextWriterTag.Input;
  161. }
  162. }
  163. [MonoTODO("Check_Value_of_Text_Potential_Bug_In_MS_Implementation")]
  164. protected override void AddAttributesToRender(HtmlTextWriter writer)
  165. {
  166. if(Page != null)
  167. {
  168. Page.VerifyRenderingInServerForm(this);
  169. }
  170. writer.AddAttribute(HtmlTextWriterAttribute.Name, UniqueID);
  171. if(TextMode == TextBoxMode.MultiLine)
  172. {
  173. if(Rows > 0)
  174. {
  175. writer.AddAttribute(HtmlTextWriterAttribute.Rows, Rows.ToString(NumerFormatInfo.InvariantInfo));
  176. }
  177. if(Columns > 0)
  178. {
  179. writer.AddAttribute(HtmlTextWriterAttribute.Cols, Columns.ToString(NumerFormatInfo.InvariantInfo));
  180. }
  181. if(!Wrap)
  182. {
  183. writer.AddAttribute(HtmlTextWriterAttribute.Wrap, "off");
  184. }
  185. } else
  186. {
  187. if(TextMode == TextBoxMode.Password)
  188. {
  189. writer.AddAttribute(HtmlTextWriterAttribute.Type, "password");
  190. } else
  191. {
  192. writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
  193. }
  194. if(MaxLength > 0)
  195. {
  196. writer.AddAttribute(HtmlTextWriterAttribute.Maxlength, MaxLength.ToString(NumberFormatInfo.InvariantInfo));
  197. }
  198. if(Columns > 0)
  199. {
  200. writer.AddAttribute(HtmlTextWriterAttribute.Size, Columns.ToString(NumerFormatInfo.InvariantInfo));
  201. }
  202. }
  203. writer.AddAttribute(HtmlTextWriterAttribute.Value, Text);
  204. if(ReadOnly)
  205. {
  206. writer.AddAttribute(HtmlTextWriterAttribute.ReadOnly, "readonly");
  207. }
  208. base.AddAttributesToRender(writer);
  209. if(AutoPostBack && Page != null)
  210. {
  211. writer.AddAttribute(HtmlTextWriterAttribute.Onchange, Page.GetPostBackClientEvent(this, ""));
  212. writer.AddAttribute("language", "javascript");
  213. }
  214. }
  215. protected override void AddParsedSubObject(object obj)
  216. {
  217. if(obj is LiteralControl)
  218. {
  219. Text = ((LiteralControl)obj).Text;
  220. return;
  221. }
  222. throw new HttpException(HttpRuntime.FormatResourceString("Cannot_Have_Children_Of_Type", "TextBox", GetType().Name.ToString()));
  223. }
  224. [MonoTODO("OnPreRender")]
  225. protected override void OnPreRender(EventArgs e)
  226. {
  227. OnPreRender(e);
  228. throw new NotImplementedException();
  229. }
  230. protected virtual void OnTextChanged(EventArgs e)
  231. {
  232. if(Events != null)
  233. {
  234. EventHandler eh = (EventHandler)(Events[TextChangedEvent]);
  235. if(eh != null)
  236. eh(this, e);
  237. }
  238. }
  239. [MonoTODO("Encode_Text")]
  240. protected override void Render(HtmlTextWriter writer)
  241. {
  242. RenderBeginTag(writer);
  243. //TODO: if(TextMode == MultiLine) { Encode(Text) and writeTo(writer) }
  244. RenderEndTag(writer);
  245. throw new NotImplementedException();
  246. }
  247. bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
  248. {
  249. if(postCollection[postDataKey] != Text)
  250. {
  251. Text = postCollection[postDataKey];
  252. return true;
  253. }
  254. return false;
  255. }
  256. void IPostBackDataHandler.RaisePostDataChangedEvent()
  257. {
  258. OnTextChanged(EventArgs.Empty);
  259. }
  260. }
  261. }