TextBox.cs 14 KB

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