TextBox.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. using System.Security.Permissions;
  31. using System.Text;
  32. namespace System.Web.UI.WebControls {
  33. // CAS
  34. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  35. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  36. // attributes
  37. [DataBindingHandler ("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
  38. [DefaultEvent ("TextChanged")]
  39. [DefaultProperty ("Text")]
  40. [ValidationProperty ("Text")]
  41. [ControlBuilder (typeof (TextBoxControlBuilder))]
  42. [Designer ("System.Web.UI.Design.WebControls.PreviewControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  43. [ParseChildren (true, "Text")]
  44. [ControlValueProperty ("Text", null)]
  45. [SupportsEventValidation]
  46. public class TextBox : WebControl, IPostBackDataHandler
  47. , IEditableTextControl, ITextControl
  48. {
  49. readonly static string [] VCardValues = new string [] {
  50. null,
  51. null,
  52. "vCard.Cellular",
  53. "vCard.Company",
  54. "vCard.Department",
  55. "vCard.DisplayName",
  56. "vCard.Email",
  57. "vCard.FirstName",
  58. "vCard.Gender",
  59. "vCard.Home.City",
  60. "HomeCountry",
  61. "vCard.Home.Fax",
  62. "vCard.Home.Phone",
  63. "vCard.Home.State",
  64. "vCard.Home.StreetAddress",
  65. "vCard.Home.ZipCode",
  66. "vCard.Home.page",
  67. "vCard.JobTitle",
  68. "vCard.LastName",
  69. "vCard.MiddleName",
  70. "vCard.Notes",
  71. "vCard.Office",
  72. "vCard.Pager",
  73. "vCard.Business.City",
  74. "BusinessCountry",
  75. "vCard.Business.Fax",
  76. "vCard.Business.Phone",
  77. "vCard.Business.State",
  78. "vCard.Business.StreetAddress",
  79. "vCard.Business.Url",
  80. "vCard.Business.ZipCode",
  81. "search"
  82. };
  83. protected override void AddAttributesToRender (HtmlTextWriter w)
  84. {
  85. Page page = Page;
  86. if (page != null)
  87. page.VerifyRenderingInServerForm (this);
  88. switch (TextMode) {
  89. case TextBoxMode.MultiLine:
  90. if (Columns != 0)
  91. w.AddAttribute (HtmlTextWriterAttribute.Cols, Columns.ToString (), false);
  92. else
  93. w.AddAttribute (HtmlTextWriterAttribute.Cols, "20", false);
  94. if (Rows != 0)
  95. w.AddAttribute (HtmlTextWriterAttribute.Rows, Rows.ToString (), false);
  96. else
  97. w.AddAttribute (HtmlTextWriterAttribute.Rows, "2", false);
  98. if (!Wrap)
  99. w.AddAttribute (HtmlTextWriterAttribute.Wrap, "off", false);
  100. break;
  101. case TextBoxMode.SingleLine:
  102. case TextBoxMode.Password:
  103. if (TextMode == TextBoxMode.Password)
  104. w.AddAttribute (HtmlTextWriterAttribute.Type, "password", false);
  105. else {
  106. w.AddAttribute (HtmlTextWriterAttribute.Type, "text", false);
  107. if (Text.Length > 0)
  108. w.AddAttribute (HtmlTextWriterAttribute.Value, Text);
  109. }
  110. if (Columns != 0)
  111. w.AddAttribute (HtmlTextWriterAttribute.Size, Columns.ToString (), false);
  112. if (MaxLength != 0)
  113. w.AddAttribute (HtmlTextWriterAttribute.Maxlength, MaxLength.ToString (), false);
  114. if (AutoCompleteType != AutoCompleteType.None && TextMode == TextBoxMode.SingleLine)
  115. if (AutoCompleteType != AutoCompleteType.Disabled)
  116. w.AddAttribute (HtmlTextWriterAttribute.VCardName, VCardValues [(int) AutoCompleteType]);
  117. else
  118. w.AddAttribute (HtmlTextWriterAttribute.AutoComplete, "off", false);
  119. break;
  120. }
  121. if (AutoPostBack) {
  122. w.AddAttribute ("onkeypress", "if (WebForm_TextBoxKeyHandler(event) == false) return false;", false);
  123. if (page != null) {
  124. string onchange = page.ClientScript.GetPostBackEventReference (GetPostBackOptions (), true);
  125. onchange = String.Concat ("setTimeout('", onchange.Replace ("\\", "\\\\").Replace ("'", "\\'"), "', 0)");
  126. w.AddAttribute (HtmlTextWriterAttribute.Onchange, BuildScriptAttribute ("onchange", onchange));
  127. }
  128. } else if (page != null)
  129. page.ClientScript.RegisterForEventValidation (UniqueID, String.Empty);
  130. if (ReadOnly)
  131. w.AddAttribute (HtmlTextWriterAttribute.ReadOnly, "ReadOnly", false);
  132. w.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
  133. base.AddAttributesToRender (w);
  134. }
  135. protected override void AddParsedSubObject (object obj)
  136. {
  137. LiteralControl l = obj as LiteralControl;
  138. if (l != null)
  139. Text = l.Text;
  140. }
  141. protected internal
  142. override void OnPreRender (EventArgs e)
  143. {
  144. // What do i do here?
  145. base.OnPreRender (e);
  146. if (AutoPostBack) {
  147. RegisterKeyHandlerClientScript ();
  148. }
  149. Page page = Page;
  150. if (page != null && IsEnabled)
  151. page.RegisterEnabledControl (this);
  152. }
  153. protected internal
  154. override void Render (HtmlTextWriter w)
  155. {
  156. // Why didn't msft just override RenderContents!?
  157. RenderBeginTag (w);
  158. if (TextMode == TextBoxMode.MultiLine) {
  159. w.WriteLine ();
  160. HttpUtility.HtmlEncode (Text, w);
  161. }
  162. RenderEndTag (w);
  163. }
  164. protected virtual
  165. bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  166. {
  167. ValidateEvent (postDataKey, String.Empty);
  168. if (Text != postCollection [postDataKey]) {
  169. Text = postCollection [postDataKey];
  170. return true;
  171. }
  172. return false;
  173. }
  174. protected virtual
  175. void RaisePostDataChangedEvent ()
  176. {
  177. if (CausesValidation)
  178. Page.Validate (ValidationGroup);
  179. OnTextChanged (EventArgs.Empty);
  180. }
  181. bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
  182. {
  183. return LoadPostData (postDataKey, postCollection);
  184. }
  185. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  186. {
  187. RaisePostDataChangedEvent ();
  188. }
  189. protected override object SaveViewState ()
  190. {
  191. if (TextMode == TextBoxMode.Password)
  192. ViewState.SetItemDirty ("Text", false);
  193. return base.SaveViewState ();
  194. }
  195. PostBackOptions GetPostBackOptions () {
  196. PostBackOptions options = new PostBackOptions (this);
  197. options.ActionUrl = null;
  198. options.ValidationGroup = null;
  199. options.Argument = String.Empty;
  200. options.RequiresJavaScriptProtocol = false;
  201. options.ClientSubmit = true;
  202. Page page = Page;
  203. options.PerformValidation = CausesValidation && page != null && page.AreValidatorsUplevel (ValidationGroup);
  204. if (options.PerformValidation)
  205. options.ValidationGroup = ValidationGroup;
  206. return options;
  207. }
  208. void RegisterKeyHandlerClientScript () {
  209. if (!Page.ClientScript.IsClientScriptBlockRegistered (typeof (TextBox), "KeyHandler")) {
  210. StringBuilder script=new StringBuilder();
  211. script.AppendLine ("function WebForm_TextBoxKeyHandler(event) {");
  212. script.AppendLine ("\tvar target = event.target;");
  213. script.AppendLine ("\tif ((target == null) || (typeof(target) == \"undefined\")) target = event.srcElement;");
  214. script.AppendLine ("\tif (event.keyCode == 13) {");
  215. script.AppendLine ("\t\tif ((typeof(target) != \"undefined\") && (target != null)) {");
  216. script.AppendLine ("\t\t\tif (typeof(target.onchange) != \"undefined\") {");
  217. script.AppendLine ("\t\t\t\ttarget.onchange();");
  218. script.AppendLine ("\t\t\t\tevent.cancelBubble = true;");
  219. script.AppendLine ("\t\t\t\tif (event.stopPropagation) event.stopPropagation();");
  220. script.AppendLine ("\t\t\t\treturn false;");
  221. script.AppendLine ("\t\t\t}");
  222. script.AppendLine ("\t\t}");
  223. script.AppendLine ("\t}");
  224. script.AppendLine ("\treturn true;");
  225. script.AppendLine ("}");
  226. Page.ClientScript.RegisterClientScriptBlock (typeof (TextBox), "KeyHandler", script.ToString(), true);
  227. }
  228. }
  229. [DefaultValue (AutoCompleteType.None)]
  230. [Themeable (false)]
  231. public virtual AutoCompleteType AutoCompleteType
  232. {
  233. get {
  234. object o = ViewState ["AutoCompleteType"];
  235. return o != null ? (AutoCompleteType) o : AutoCompleteType.None;
  236. }
  237. set {
  238. ViewState ["AutoCompleteType"] = value;
  239. }
  240. }
  241. [DefaultValue(false)]
  242. [Themeable (false)]
  243. [WebSysDescription ("")]
  244. [WebCategory ("Behavior")]
  245. public virtual bool AutoPostBack {
  246. get {
  247. return ViewState.GetBool ("AutoPostBack", false);
  248. }
  249. set {
  250. ViewState ["AutoPostBack"] = value;
  251. }
  252. }
  253. [DefaultValue (false)]
  254. [Themeable (false)]
  255. public virtual bool CausesValidation
  256. {
  257. get {
  258. return ViewState.GetBool ("CausesValidation", false);
  259. }
  260. set {
  261. ViewState["CausesValidation"] = value;
  262. }
  263. }
  264. [DefaultValue(0)]
  265. [WebSysDescription ("")]
  266. [WebCategory ("Appearance")]
  267. public virtual int Columns {
  268. get {
  269. return ViewState.GetInt ("Columns", 0);
  270. }
  271. set {
  272. if (value < 0)
  273. throw new ArgumentOutOfRangeException("value", "Columns value has to be 0 for 'not set' or bigger than 0.");
  274. else
  275. ViewState ["Columns"] = value;
  276. }
  277. }
  278. [DefaultValue(0)]
  279. [Themeable (false)]
  280. [WebSysDescription ("")]
  281. [WebCategory ("Behavior")]
  282. public virtual int MaxLength {
  283. get {
  284. return ViewState.GetInt ("MaxLength", 0);
  285. }
  286. set {
  287. if (value < 0)
  288. throw new ArgumentOutOfRangeException("value", "MaxLength value has to be 0 for 'not set' or bigger than 0.");
  289. else
  290. ViewState ["MaxLength"] = value;
  291. }
  292. }
  293. [Bindable(true)]
  294. [DefaultValue(false)]
  295. [Themeable (false)]
  296. [WebSysDescription ("")]
  297. [WebCategory ("Behavior")]
  298. public virtual bool ReadOnly {
  299. get {
  300. return ViewState.GetBool ("ReadOnly", false);
  301. }
  302. set {
  303. ViewState ["ReadOnly"] = value;
  304. }
  305. }
  306. [DefaultValue(0)]
  307. [Themeable (false)]
  308. [WebSysDescription ("")]
  309. [WebCategory ("Behavior")]
  310. public virtual int Rows {
  311. get {
  312. return ViewState.GetInt ("Rows", 0);
  313. }
  314. set {
  315. if (value < 0)
  316. throw new ArgumentOutOfRangeException("value", "Rows value has to be 0 for 'not set' or bigger than 0.");
  317. else
  318. ViewState ["Rows"] = value;
  319. }
  320. }
  321. #if HAVE_CONTROL_ADAPTERS
  322. protected virtual new
  323. #else
  324. protected override
  325. #endif
  326. HtmlTextWriterTag TagKey {
  327. get {
  328. return TextMode == TextBoxMode.MultiLine ? HtmlTextWriterTag.Textarea : HtmlTextWriterTag.Input;
  329. }
  330. }
  331. [Bindable(true, BindingDirection.TwoWay)]
  332. [DefaultValue("")]
  333. [PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)]
  334. [Localizable (true)]
  335. [Editor ("System.ComponentModel.Design.MultilineStringEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  336. [WebSysDescription ("")]
  337. [WebCategory ("Appearance")]
  338. public virtual string Text {
  339. get {
  340. return ViewState.GetString ("Text", "");
  341. }
  342. set {
  343. ViewState ["Text"] = value;
  344. }
  345. }
  346. [DefaultValue(TextBoxMode.SingleLine)]
  347. [Themeable (false)]
  348. [WebSysDescription ("")]
  349. [WebCategory ("Behavior")]
  350. public virtual TextBoxMode TextMode {
  351. get {
  352. return (TextBoxMode) ViewState.GetInt ("TextMode", (int) TextBoxMode.SingleLine);
  353. }
  354. set {
  355. ViewState ["TextMode"] = (int) value;
  356. }
  357. }
  358. [Themeable (false)]
  359. [DefaultValue ("")]
  360. public virtual string ValidationGroup
  361. {
  362. get {
  363. return ViewState.GetString ("ValidationGroup", "");
  364. }
  365. set {
  366. ViewState ["ValidationGroup"] = value;
  367. }
  368. }
  369. [DefaultValue(true)]
  370. [WebSysDescription ("")]
  371. [WebCategory ("Layout")]
  372. public virtual bool Wrap {
  373. get {
  374. return ViewState.GetBool ("Wrap", true);
  375. }
  376. set {
  377. ViewState ["Wrap"] = value;
  378. }
  379. }
  380. protected virtual void OnTextChanged (EventArgs e)
  381. {
  382. EventHandler h = (EventHandler) Events [TextChangedEvent];
  383. if (h != null)
  384. h (this, e);
  385. }
  386. static readonly object TextChangedEvent = new object ();
  387. [WebSysDescription ("")]
  388. [WebCategory ("Action")]
  389. public event EventHandler TextChanged {
  390. add { Events.AddHandler (TextChangedEvent, value); }
  391. remove { Events.RemoveHandler (TextChangedEvent, value); }
  392. }
  393. }
  394. }