TextBox.cs 13 KB

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