2
0

TextBox.cs 5.7 KB

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