TextBox.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. //
  2. // System.Web.UI.WebControls.TextBox.cs
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.Collections.Specialized;
  29. using System.ComponentModel;
  30. namespace System.Web.UI.WebControls {
  31. [DataBindingHandler ("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
  32. [DefaultEvent ("TextChanged")]
  33. [DefaultProperty ("Text")]
  34. [ValidationProperty ("Text")]
  35. [ControlBuilder (typeof (TextBoxControlBuilder))]
  36. #if NET_2_0
  37. [Designer ("System.Web.UI.Design.WebControls.PreviewControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  38. [ParseChildren (true, "Text")]
  39. [ControlValueProperty ("Text", null)]
  40. #else
  41. [ParseChildren (false)]
  42. #endif
  43. public class TextBox : WebControl, IPostBackDataHandler
  44. #if NET_2_0
  45. , IEditableTextControl, ITextControl
  46. #endif
  47. {
  48. protected override void AddAttributesToRender (HtmlTextWriter w)
  49. {
  50. if (Page != null)
  51. Page.VerifyRenderingInServerForm (this);
  52. base.AddAttributesToRender (w);
  53. switch (TextMode) {
  54. case TextBoxMode.MultiLine:
  55. if (Columns != 0)
  56. w.AddAttribute (HtmlTextWriterAttribute.Cols, Columns.ToString ());
  57. if (Rows != 0)
  58. w.AddAttribute (HtmlTextWriterAttribute.Rows, Rows.ToString ());
  59. if (!Wrap)
  60. w.AddAttribute (HtmlTextWriterAttribute.Wrap, "off");
  61. break;
  62. case TextBoxMode.SingleLine:
  63. case TextBoxMode.Password:
  64. if (TextMode == TextBoxMode.Password)
  65. w.AddAttribute (HtmlTextWriterAttribute.Type, "password");
  66. else {
  67. w.AddAttribute (HtmlTextWriterAttribute.Type, "text");
  68. w.AddAttribute (HtmlTextWriterAttribute.Value, Text);
  69. }
  70. if (Columns != 0)
  71. w.AddAttribute (HtmlTextWriterAttribute.Size, Columns.ToString ());
  72. if (MaxLength != 0)
  73. w.AddAttribute (HtmlTextWriterAttribute.Maxlength, MaxLength.ToString ());
  74. break;
  75. }
  76. if (AutoPostBack)
  77. w.AddAttribute (HtmlTextWriterAttribute.Onchange, Page.ClientScript.GetPostBackClientHyperlink (this, ""));
  78. if (ReadOnly)
  79. w.AddAttribute (HtmlTextWriterAttribute.ReadOnly, "ReadOnly");
  80. w.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
  81. }
  82. protected override void AddParsedSubObject (object obj)
  83. {
  84. LiteralControl l = obj as LiteralControl;
  85. if (l != null)
  86. Text = l.Text;
  87. }
  88. [MonoTODO]
  89. #if NET_2_0
  90. protected internal
  91. #else
  92. protected
  93. #endif
  94. override void OnPreRender (EventArgs e)
  95. {
  96. // What do i do here?
  97. base.OnPreRender (e);
  98. }
  99. [MonoTODO ("Am I missing something here")]
  100. #if NET_2_0
  101. protected internal
  102. #else
  103. protected
  104. #endif
  105. override void Render (HtmlTextWriter w)
  106. {
  107. // Why didn't msft just override RenderContents!?
  108. RenderBeginTag (w);
  109. if (TextMode == TextBoxMode.MultiLine)
  110. HttpUtility.HtmlEncode (Text, w);
  111. RenderEndTag (w);
  112. }
  113. #if NET_2_0
  114. [MonoTODO]
  115. protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  116. {
  117. throw new NotImplementedException ();
  118. }
  119. [MonoTODO]
  120. protected virtual void RaisePostDataChangedEvent ()
  121. {
  122. throw new NotImplementedException ();
  123. }
  124. #endif
  125. bool IPostBackDataHandler.LoadPostData (string key, NameValueCollection col)
  126. {
  127. if (Text != col [key]) {
  128. Text = col [key];
  129. return true;
  130. }
  131. return false;
  132. }
  133. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  134. {
  135. OnTextChanged (EventArgs.Empty);
  136. }
  137. #if NET_2_0
  138. [MonoTODO]
  139. protected override object SaveViewState ()
  140. {
  141. return base.SaveViewState ();
  142. }
  143. #endif
  144. #if NET_2_0
  145. [DefaultValue (AutoCompleteType.None)]
  146. [Themeable (false)]
  147. [MonoTODO]
  148. public virtual AutoCompleteType AutoCompleteType
  149. {
  150. get {
  151. throw new NotImplementedException ();
  152. }
  153. set {
  154. throw new NotImplementedException ();
  155. }
  156. }
  157. #endif
  158. [DefaultValue(false)]
  159. #if NET_2_0
  160. [Themeable (false)]
  161. #endif
  162. [WebSysDescription ("")]
  163. [WebCategory ("Behavior")]
  164. public virtual bool AutoPostBack {
  165. get {
  166. return ViewState.GetBool ("AutoPostBack", false);
  167. }
  168. set {
  169. ViewState ["AutoPostBack"] = value;
  170. }
  171. }
  172. #if NET_2_0
  173. [DefaultValue (false)]
  174. [Themeable (false)]
  175. public virtual bool CausesValidation
  176. {
  177. get {
  178. return ViewState.GetBool ("CausesValidation", false);
  179. }
  180. set {
  181. ViewState["CausesValidation"] = value;
  182. }
  183. }
  184. #endif
  185. #if ONLY_1_1
  186. [Bindable(true)]
  187. #endif
  188. [DefaultValue(0)]
  189. [WebSysDescription ("")]
  190. [WebCategory ("Appearance")]
  191. public virtual int Columns {
  192. get {
  193. return ViewState.GetInt ("Columns", 0);
  194. }
  195. set {
  196. ViewState ["Columns"] = value;
  197. }
  198. }
  199. #if ONLY_1_1
  200. [Bindable(true)]
  201. #endif
  202. [DefaultValue(0)]
  203. #if NET_2_0
  204. [Themeable (false)]
  205. #endif
  206. [WebSysDescription ("")]
  207. [WebCategory ("Behavior")]
  208. public virtual int MaxLength {
  209. get {
  210. return ViewState.GetInt ("MaxLength", 0);
  211. }
  212. set {
  213. ViewState ["MaxLength"] = value;
  214. }
  215. }
  216. [Bindable(true)]
  217. [DefaultValue(false)]
  218. #if NET_2_0
  219. [Themeable (false)]
  220. #endif
  221. [WebSysDescription ("")]
  222. [WebCategory ("Behavior")]
  223. public virtual bool ReadOnly {
  224. get {
  225. return ViewState.GetBool ("ReadOnly", false);
  226. }
  227. set {
  228. ViewState ["ReadOnly"] = value;
  229. }
  230. }
  231. #if ONLY_1_1
  232. [Bindable(true)]
  233. #endif
  234. [DefaultValue(0)]
  235. #if NET_2_0
  236. [Themeable (false)]
  237. #endif
  238. [WebSysDescription ("")]
  239. [WebCategory ("Behavior")]
  240. public virtual int Rows {
  241. get {
  242. return ViewState.GetInt ("Rows", 0);
  243. }
  244. set {
  245. ViewState ["Rows"] = value;
  246. }
  247. }
  248. #if NET_2_0 && HAVE_CONTROL_ADAPTERS
  249. protected virtual new
  250. #else
  251. protected override
  252. #endif
  253. HtmlTextWriterTag TagKey {
  254. get {
  255. return TextMode == TextBoxMode.MultiLine ? HtmlTextWriterTag.Textarea : HtmlTextWriterTag.Input;
  256. }
  257. }
  258. #if NET_2_0
  259. [Bindable(true, BindingDirection.TwoWay)]
  260. #else
  261. [Bindable(true)]
  262. #endif
  263. [DefaultValue("")]
  264. [PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)]
  265. #if NET_2_0
  266. [Localizable (true)]
  267. [Editor ("System.ComponentModel.Design.MultilineStringEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  268. #endif
  269. [WebSysDescription ("")]
  270. [WebCategory ("Appearance")]
  271. public virtual string Text {
  272. get {
  273. return ViewState.GetString ("Text", "");
  274. }
  275. set {
  276. ViewState ["Text"] = value;
  277. if (TextMode == TextBoxMode.Password)
  278. ViewState.SetItemDirty ("Text", false);
  279. }
  280. }
  281. [DefaultValue(TextBoxMode.SingleLine)]
  282. #if NET_2_0
  283. [Themeable (false)]
  284. #endif
  285. [WebSysDescription ("")]
  286. [WebCategory ("Behavior")]
  287. public virtual TextBoxMode TextMode {
  288. get {
  289. return (TextBoxMode) ViewState.GetInt ("TextMode", (int) TextBoxMode.SingleLine);
  290. }
  291. set {
  292. ViewState ["TextMode"] = (int) value;
  293. }
  294. }
  295. #if NET_2_0
  296. [Themeable (false)]
  297. [DefaultValue ("")]
  298. public string ValidationGroup
  299. {
  300. get {
  301. return ViewState.GetString ("ValidationGroup", "");
  302. }
  303. set {
  304. ViewState ["ValidationGroup"] = value;
  305. }
  306. }
  307. #endif
  308. [DefaultValue(true)]
  309. [WebSysDescription ("")]
  310. [WebCategory ("Layout")]
  311. public virtual bool Wrap {
  312. get {
  313. return ViewState.GetBool ("Wrap", true);
  314. }
  315. set {
  316. ViewState ["Wrap"] = value;
  317. }
  318. }
  319. protected virtual void OnTextChanged (EventArgs e)
  320. {
  321. EventHandler h = (EventHandler) Events [TextChangedEvent];
  322. if (h != null)
  323. h (this, e);
  324. }
  325. static readonly object TextChangedEvent = new object ();
  326. [WebSysDescription ("")]
  327. [WebCategory ("Action")]
  328. public event EventHandler TextChanged {
  329. add { Events.AddHandler (TextChangedEvent, value); }
  330. remove { Events.RemoveHandler (TextChangedEvent, value); }
  331. }
  332. }
  333. }