HtmlInputText.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // System.Web.UI.HtmlControls.HtmlInputText.cs
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (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.ComponentModel;
  29. using System.Collections.Specialized;
  30. using System.Globalization;
  31. using System.Security.Permissions;
  32. namespace System.Web.UI.HtmlControls {
  33. // CAS
  34. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  35. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  36. // attributes
  37. [DefaultEvent ("ServerChange")]
  38. [ValidationProperty ("Value")]
  39. #if NET_2_0
  40. [SupportsEventValidation]
  41. #endif
  42. public class HtmlInputText : HtmlInputControl, IPostBackDataHandler
  43. {
  44. static readonly object serverChangeEvent = new object ();
  45. public HtmlInputText ()
  46. : base ("text")
  47. {
  48. }
  49. public HtmlInputText (string type)
  50. : base (type)
  51. {
  52. }
  53. [DefaultValue ("")]
  54. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  55. [WebSysDescription("")]
  56. [WebCategory("Behavior")]
  57. public int MaxLength {
  58. get {
  59. string s = Attributes ["maxlength"];
  60. return (s == null) ? -1 : Convert.ToInt32 (s);
  61. }
  62. set {
  63. if (value == -1)
  64. Attributes.Remove ("maxlength");
  65. else
  66. Attributes ["maxlength"] = value.ToString ();
  67. }
  68. }
  69. #if NET_2_0
  70. [DefaultValue (-1)]
  71. #else
  72. [DefaultValue ("")]
  73. #endif
  74. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  75. [WebSysDescription("")]
  76. [WebCategory("Appearance")]
  77. public int Size {
  78. get {
  79. string s = Attributes ["size"];
  80. return (s == null) ? -1 : Convert.ToInt32 (s);
  81. }
  82. set {
  83. if (value == -1)
  84. Attributes.Remove ("size");
  85. else
  86. Attributes ["size"] = value.ToString ();
  87. }
  88. }
  89. public override string Value {
  90. get {
  91. string s = Attributes ["value"];
  92. return (s == null) ? String.Empty : s;
  93. }
  94. set {
  95. if (value == null)
  96. Attributes.Remove ("value");
  97. else
  98. Attributes ["value"] = value;
  99. }
  100. }
  101. #if NET_2_0
  102. protected internal override void Render (HtmlTextWriter writer)
  103. {
  104. Page page = Page;
  105. if (page != null)
  106. page.ClientScript.RegisterForEventValidation (UniqueID);
  107. base.Render (writer);
  108. }
  109. #endif
  110. #if NET_2_0
  111. protected internal
  112. #else
  113. protected
  114. #endif
  115. override void OnPreRender (EventArgs e)
  116. {
  117. base.OnPreRender (e);
  118. if (Page != null && !Disabled) {
  119. Page.RegisterRequiresPostBack (this);
  120. #if NET_2_0
  121. Page.RegisterEnabledControl (this);
  122. #endif
  123. }
  124. }
  125. protected virtual void OnServerChange (EventArgs e)
  126. {
  127. EventHandler serverChange = (EventHandler) Events [serverChangeEvent];
  128. if (serverChange != null)
  129. serverChange (this, e);
  130. }
  131. protected override void RenderAttributes (HtmlTextWriter writer)
  132. {
  133. // the Type property can be, indirectly, changed by using the Attributes property
  134. if (String.Compare (Type, 0, "password", 0, 8, true, CultureInfo.InvariantCulture) == 0) {
  135. Attributes.Remove ("value");
  136. }
  137. base.RenderAttributes (writer);
  138. }
  139. bool LoadPostDataInternal (string postDataKey, NameValueCollection postCollection)
  140. {
  141. string s = postCollection [postDataKey];
  142. if (Value != s) {
  143. Value = s;
  144. return true;
  145. }
  146. return false;
  147. }
  148. void RaisePostDataChangedEventInternal ()
  149. {
  150. OnServerChange (EventArgs.Empty);
  151. }
  152. #if NET_2_0
  153. protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  154. {
  155. return LoadPostDataInternal (postDataKey, postCollection);
  156. }
  157. protected virtual void RaisePostDataChangedEvent ()
  158. {
  159. ValidateEvent (UniqueID, String.Empty);
  160. RaisePostDataChangedEventInternal ();
  161. }
  162. #endif
  163. bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
  164. {
  165. #if NET_2_0
  166. return LoadPostData (postDataKey, postCollection);
  167. #else
  168. return LoadPostDataInternal (postDataKey, postCollection);
  169. #endif
  170. }
  171. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  172. {
  173. #if NET_2_0
  174. RaisePostDataChangedEvent ();
  175. #else
  176. RaisePostDataChangedEventInternal ();
  177. #endif
  178. }
  179. [WebSysDescription("")]
  180. [WebCategory("Action")]
  181. public event EventHandler ServerChange {
  182. add { Events.AddHandler (serverChangeEvent, value); }
  183. remove { Events.RemoveHandler (serverChangeEvent, value); }
  184. }
  185. }
  186. }