CheckBoxField.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 ApplyFormatInEditMode {
  44. get { throw GetNotSupportedPropException ("ApplyFormatInEditMode"); }
  45. set { throw GetNotSupportedPropException ("ApplyFormatInEditMode"); }
  46. }
  47. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  48. [BrowsableAttribute (false)]
  49. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  50. public override bool ConvertEmptyStringToNull {
  51. get { throw GetNotSupportedPropException ("ConvertEmptyStringToNull"); }
  52. set { throw GetNotSupportedPropException ("ConvertEmptyStringToNull"); }
  53. }
  54. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  55. [BrowsableAttribute (false)]
  56. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  57. public override string DataFormatString {
  58. get { throw GetNotSupportedPropException ("DataFormatString"); }
  59. set { throw GetNotSupportedPropException ("DataFormatString"); }
  60. }
  61. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  62. [BrowsableAttribute (false)]
  63. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  64. public override bool HtmlEncode {
  65. get { throw GetNotSupportedPropException ("HtmlEncode"); }
  66. set { throw GetNotSupportedPropException ("HtmlEncode"); }
  67. }
  68. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  69. [BrowsableAttribute (false)]
  70. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  71. public override string NullDisplayText {
  72. get { throw GetNotSupportedPropException ("NullDisplayText"); }
  73. set { throw GetNotSupportedPropException ("NullDisplayText"); }
  74. }
  75. protected override bool SupportsHtmlEncode {
  76. get { return false; }
  77. }
  78. [LocalizableAttribute (true)]
  79. [DefaultValueAttribute ("")]
  80. [WebSysDescription ("")]
  81. [WebCategoryAttribute ("Appearance")]
  82. public virtual string Text {
  83. get { return ViewState.GetString ("Text", ""); }
  84. set {
  85. ViewState ["Text"] = value;
  86. OnFieldChanged ();
  87. }
  88. }
  89. protected override void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
  90. {
  91. bool editable = IsEditable (rowState);
  92. CheckBox box = new CheckBox ();
  93. box.Enabled = editable;
  94. if (editable)
  95. box.ToolTip = HeaderText;
  96. box.Text = Text;
  97. cell.Controls.Add (box);
  98. }
  99. public override void ExtractValuesFromCell (IOrderedDictionary dictionary,
  100. DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
  101. {
  102. bool editable = IsEditable (rowState);
  103. if (editable || includeReadOnly) {
  104. CheckBox box = (CheckBox) cell.Controls [0];
  105. dictionary [DataField] = box.Checked;
  106. }
  107. }
  108. protected override void OnDataBindField (object sender, EventArgs e)
  109. {
  110. try {
  111. Control container = (Control) sender;
  112. object val = GetValue (container.NamingContainer);
  113. CheckBox box = sender as CheckBox;
  114. if (box == null) {
  115. DataControlFieldCell cell = sender as DataControlFieldCell;
  116. if (cell != null) {
  117. ControlCollection controls = cell.Controls;
  118. int ccount = controls != null ? controls.Count : 0;
  119. if (ccount == 1)
  120. box = controls [0] as CheckBox;
  121. if (box == null)
  122. return;
  123. }
  124. }
  125. if (box == null)
  126. throw new HttpException ("CheckBox field '" + DataField + "' contains a control that isn't a CheckBox. Override OnDataBindField to inherit from CheckBoxField and add different controls.");
  127. if (val != null && val != DBNull.Value)
  128. box.Checked = (bool) val;
  129. else
  130. if (string.IsNullOrEmpty (DataField)) {
  131. box.Visible = false;
  132. return;
  133. }
  134. if (!box.Visible)
  135. box.Visible = true;
  136. } catch (HttpException) {
  137. throw;
  138. } catch (Exception ex) {
  139. throw new HttpException (ex.Message, ex);
  140. }
  141. }
  142. protected override object GetDesignTimeValue ()
  143. {
  144. return true;
  145. }
  146. protected override DataControlField CreateField ()
  147. {
  148. return new CheckBoxField ();
  149. }
  150. protected override void CopyProperties (DataControlField newField)
  151. {
  152. CheckBoxField field = (CheckBoxField) newField;
  153. field.DataField = DataField;
  154. field.ReadOnly = ReadOnly;
  155. field.Text = Text;
  156. }
  157. }
  158. }
  159. #endif