CheckBoxField.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
  92. CheckBox box = new CheckBox ();
  93. box.Enabled = editable && !ReadOnly;
  94. box.ToolTip = HeaderText;
  95. box.Text = Text;
  96. cell.Controls.Add (box);
  97. }
  98. public override void ExtractValuesFromCell (IOrderedDictionary dictionary,
  99. DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
  100. {
  101. CheckBox box = (CheckBox) cell.Controls [0];
  102. dictionary [DataField] = box.Checked;
  103. }
  104. protected override void OnDataBindField (object sender, EventArgs e)
  105. {
  106. try {
  107. DataControlFieldCell cell = (DataControlFieldCell) sender;
  108. CheckBox box = (CheckBox) cell.Controls [0];
  109. object val = GetValue (cell.BindingContainer);
  110. if (val != null) {
  111. box.Checked = (bool) val;
  112. if (!box.Visible)
  113. box.Visible = true;
  114. }
  115. else
  116. box.Visible = false;
  117. }
  118. catch (HttpException) {
  119. throw;
  120. }
  121. catch (Exception ex) {
  122. throw new HttpException (ex.Message, ex);
  123. }
  124. }
  125. protected override object GetDesignTimeValue ()
  126. {
  127. return true;
  128. }
  129. protected override DataControlField CreateField ()
  130. {
  131. return new CheckBoxField ();
  132. }
  133. protected override void CopyProperties (DataControlField newField)
  134. {
  135. CheckBoxField field = (CheckBoxField) newField;
  136. field.DataField = DataField;
  137. field.ReadOnly = ReadOnly;
  138. field.Text = Text;
  139. }
  140. }
  141. }
  142. #endif