CheckBoxField.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // System.Web.UI.WebControls.CheckBoxField.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System.Collections;
  31. using System.Collections.Specialized;
  32. using System.Web.UI;
  33. using System.ComponentModel;
  34. using System.Security.Permissions;
  35. namespace System.Web.UI.WebControls {
  36. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  37. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  38. public class CheckBoxField : BoundField
  39. {
  40. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  41. [BrowsableAttribute (false)]
  42. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  43. public override bool ConvertEmptyStringToNull {
  44. get { throw GetNotSupportedPropException ("ConvertEmptyStringToNull"); }
  45. set { throw GetNotSupportedPropException ("ConvertEmptyStringToNull"); }
  46. }
  47. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  48. [BrowsableAttribute (false)]
  49. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  50. public override string DataFormatString {
  51. get { throw GetNotSupportedPropException ("DataFormatString"); }
  52. set { throw GetNotSupportedPropException ("DataFormatString"); }
  53. }
  54. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  55. [BrowsableAttribute (false)]
  56. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  57. public override bool HtmlEncode {
  58. get { throw GetNotSupportedPropException ("HtmlEncode"); }
  59. set { throw GetNotSupportedPropException ("HtmlEncode"); }
  60. }
  61. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  62. [BrowsableAttribute (false)]
  63. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  64. public override string NullDisplayText {
  65. get { throw GetNotSupportedPropException ("NullDisplayText"); }
  66. set { throw GetNotSupportedPropException ("NullDisplayText"); }
  67. }
  68. protected override bool SupportsHtmlEncode {
  69. get { return false; }
  70. }
  71. [LocalizableAttribute (true)]
  72. [DefaultValueAttribute ("")]
  73. [WebSysDescription ("")]
  74. [WebCategoryAttribute ("Appearance")]
  75. public virtual string Text {
  76. get {
  77. object ob = ViewState ["Text"];
  78. if (ob != null) return (string) ob;
  79. return "";
  80. }
  81. set {
  82. ViewState ["Text"] = value;
  83. OnFieldChanged ();
  84. }
  85. }
  86. public override void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
  87. {
  88. bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
  89. CheckBox box = new CheckBox ();
  90. box.Enabled = editable && !ReadOnly;
  91. cell.Controls.Add (box);
  92. }
  93. public override void ExtractValuesFromCell (IOrderedDictionary dictionary,
  94. DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
  95. {
  96. CheckBox box = (CheckBox) cell.Controls [0];
  97. dictionary [DataField] = box.Checked;
  98. }
  99. protected override void OnDataBindField (object sender, EventArgs e)
  100. {
  101. DataControlFieldCell cell = (DataControlFieldCell) sender;
  102. CheckBox box = (CheckBox) cell.Controls [0];
  103. object val = GetValue (cell.BindingContainer);
  104. if (val != null) {
  105. box.Checked = (bool)val;
  106. if (!box.Visible)
  107. box.Visible = true;
  108. }
  109. else
  110. box.Visible = false;
  111. }
  112. protected override object GetDesignTimeValue ()
  113. {
  114. return true;
  115. }
  116. protected override DataControlField CreateField ()
  117. {
  118. return new CheckBoxField ();
  119. }
  120. protected override void CopyProperties (DataControlField newField)
  121. {
  122. CheckBoxField field = (CheckBoxField) newField;
  123. field.DataField = DataField;
  124. field.ReadOnly = ReadOnly;
  125. field.Text = Text;
  126. }
  127. }
  128. }
  129. #endif