HtmlInputCheckBox.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // System.Web.UI.HtmlControls.HtmlInputCheckBox.cs
  3. //
  4. // Author:
  5. // Dick Porter <[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. namespace System.Web.UI.HtmlControls
  31. {
  32. [DefaultEvent ("ServerChange")]
  33. public class HtmlInputCheckBox : HtmlInputControl, IPostBackDataHandler
  34. {
  35. public HtmlInputCheckBox () : base ("checkbox")
  36. {
  37. }
  38. [DefaultValue ("")]
  39. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  40. #if NET_2_0
  41. //[TypeConverter (typeof(System.Web.UI.MinimizableAttributeTypeConverter))]
  42. #endif
  43. public bool Checked
  44. {
  45. get {
  46. string check = Attributes["checked"];
  47. if (check == null) {
  48. return (false);
  49. }
  50. return (true);
  51. }
  52. set {
  53. if (value == false) {
  54. Attributes.Remove ("checked");
  55. } else {
  56. Attributes["checked"] = "checked";
  57. }
  58. }
  59. }
  60. private static readonly object EventServerChange = new object ();
  61. public event EventHandler ServerChange
  62. {
  63. add {
  64. Events.AddHandler (EventServerChange, value);
  65. }
  66. remove {
  67. Events.RemoveHandler (EventServerChange, value);
  68. }
  69. }
  70. #if NET_2_0
  71. protected internal
  72. #else
  73. protected
  74. #endif
  75. override void OnPreRender (EventArgs e)
  76. {
  77. base.OnPreRender (e);
  78. if (Page != null) {
  79. Page.RegisterRequiresPostBack (this);
  80. }
  81. }
  82. protected virtual void OnServerChange (EventArgs e)
  83. {
  84. EventHandler handler = (EventHandler)Events[EventServerChange];
  85. if (handler != null) {
  86. handler (this, e);
  87. }
  88. }
  89. #if NET_2_0
  90. [MonoTODO]
  91. protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  92. {
  93. throw new NotImplementedException ();
  94. }
  95. [MonoTODO]
  96. protected virtual void RaisePostDataChangedEvent ()
  97. {
  98. throw new NotImplementedException ();
  99. }
  100. #endif
  101. bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
  102. {
  103. string postedValue = postCollection[postDataKey];
  104. bool postedBool = ((postedValue != null) &&
  105. (postedValue.Length > 0));
  106. if (Checked != postedBool) {
  107. Checked = postedBool;
  108. return (true);
  109. }
  110. return (false);
  111. }
  112. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  113. {
  114. OnServerChange (EventArgs.Empty);
  115. }
  116. }
  117. }