2 // System.Web.UI.WebControls.CheckBoxField.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
7 // (C) 2005-2010 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
32 using System.Collections.Specialized;
34 using System.ComponentModel;
35 using System.Security.Permissions;
37 namespace System.Web.UI.WebControls
39 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41 public class CheckBoxField : BoundField
43 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
44 [BrowsableAttribute (false)]
45 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
46 public override bool ApplyFormatInEditMode {
47 get { throw GetNotSupportedPropException ("ApplyFormatInEditMode"); }
48 set { throw GetNotSupportedPropException ("ApplyFormatInEditMode"); }
51 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
52 [BrowsableAttribute (false)]
53 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
54 public override bool ConvertEmptyStringToNull {
55 get { throw GetNotSupportedPropException ("ConvertEmptyStringToNull"); }
56 set { throw GetNotSupportedPropException ("ConvertEmptyStringToNull"); }
59 [TypeConverter ("System.Web.UI.Design.DataSourceBooleanViewSchemaConverter, " + Consts.AssemblySystem_Design)]
60 public override string DataField {
61 get { return base.DataField; }
62 set { base.DataField = value; }
65 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
66 [BrowsableAttribute (false)]
67 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
68 public override string DataFormatString {
69 get { throw GetNotSupportedPropException ("DataFormatString"); }
70 set { throw GetNotSupportedPropException ("DataFormatString"); }
73 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
74 [BrowsableAttribute (false)]
75 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
76 public override bool HtmlEncode {
77 get { throw GetNotSupportedPropException ("HtmlEncode"); }
78 set { throw GetNotSupportedPropException ("HtmlEncode"); }
82 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
83 [EditorBrowsable (EditorBrowsableState.Never)]
84 public override bool HtmlEncodeFormatString {
85 get { return base.HtmlEncodeFormatString; }
86 set { base.HtmlEncodeFormatString = value; }
89 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
90 [BrowsableAttribute (false)]
91 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
92 public override string NullDisplayText {
93 get { throw GetNotSupportedPropException ("NullDisplayText"); }
94 set { throw GetNotSupportedPropException ("NullDisplayText"); }
97 protected override bool SupportsHtmlEncode {
101 [LocalizableAttribute (true)]
102 [DefaultValueAttribute ("")]
103 [WebSysDescription ("")]
104 [WebCategoryAttribute ("Appearance")]
105 public virtual string Text {
106 get { return ViewState.GetString ("Text", String.Empty); }
108 ViewState ["Text"] = value;
113 protected override void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
115 bool editable = IsEditable (rowState);
116 CheckBox box = new CheckBox ();
117 box.Enabled = editable;
119 box.ToolTip = HeaderText;
121 cell.Controls.Add (box);
124 public override void ExtractValuesFromCell (IOrderedDictionary dictionary,
125 DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
127 bool editable = IsEditable (rowState);
128 if (editable || includeReadOnly) {
129 CheckBox box = (CheckBox) cell.Controls [0];
130 dictionary [DataField] = box.Checked;
134 protected override void OnDataBindField (object sender, EventArgs e)
137 Control container = (Control) sender;
138 object val = GetValue (container.NamingContainer);
139 CheckBox box = sender as CheckBox;
141 DataControlFieldCell cell = sender as DataControlFieldCell;
143 ControlCollection controls = cell.Controls;
144 int ccount = controls != null ? controls.Count : 0;
146 box = controls [0] as CheckBox;
153 throw new HttpException ("CheckBox field '" + DataField + "' contains a control that isn't a CheckBox. Override OnDataBindField to inherit from CheckBoxField and add different controls.");
155 if (val != null && val != DBNull.Value)
156 box.Checked = (bool) val;
158 if (string.IsNullOrEmpty (DataField)) {
166 } catch (HttpException) {
168 } catch (Exception ex) {
169 throw new HttpException (ex.Message, ex);
173 protected override object GetDesignTimeValue ()
178 protected override DataControlField CreateField ()
180 return new CheckBoxField ();
183 protected override void CopyProperties (DataControlField newField)
185 CheckBoxField field = (CheckBoxField) newField;
186 field.DataField = DataField;
187 field.ReadOnly = ReadOnly;
191 public override void ValidateSupportsCallback ()