2 // System.Web.UI.WebControls.BoundField.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;
36 using System.Reflection;
38 namespace System.Web.UI.WebControls
40 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42 public class BoundField : DataControlField
44 public static readonly string ThisExpression = "!";
46 //PropertyDescriptor boundProperty;
48 [DefaultValueAttribute (false)]
49 [WebSysDescription ("")]
50 [WebCategoryAttribute ("Behavior")]
51 public virtual bool ApplyFormatInEditMode {
52 get { return ViewState.GetBool ("ApplyFormatInEditMode", false); }
53 set { ViewState ["ApplyFormatInEditMode"] = value; }
56 [DefaultValueAttribute (true)]
57 [WebSysDescription ("")]
58 [WebCategoryAttribute ("Behavior")]
59 public virtual bool ConvertEmptyStringToNull {
60 get { return ViewState.GetBool ("ConvertEmptyStringToNull", true); }
62 ViewState ["ConvertEmptyStringToNull"] = value;
67 [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
68 [WebSysDescription ("")]
69 [WebCategoryAttribute ("Data")]
70 [DefaultValueAttribute ("")]
71 public virtual string DataField {
72 get { return ViewState.GetString ("DataField", String.Empty); }
74 ViewState ["DataField"] = value;
79 [DefaultValueAttribute ("")]
80 [WebSysDescription ("")]
81 [WebCategoryAttribute ("Data")]
82 public virtual string DataFormatString {
83 get { return ViewState.GetString ("DataFormatString", String.Empty); }
85 ViewState ["DataFormatString"] = value;
90 [WebSysDescription ("")]
91 [WebCategoryAttribute ("Appearance")]
92 public override string HeaderText {
93 get { return ViewState.GetString ("HeaderText", String.Empty); }
95 ViewState["HeaderText"] = value;
100 [DefaultValueAttribute ("")]
101 [WebCategoryAttribute ("Behavior")]
102 public virtual string NullDisplayText {
103 get { return ViewState.GetString ("NullDisplayText", String.Empty); }
105 ViewState ["NullDisplayText"] = value;
110 [DefaultValueAttribute (false)]
111 [WebSysDescription ("")]
112 [WebCategoryAttribute ("Behavior")]
113 public virtual bool ReadOnly {
114 get { return ViewState.GetBool ("ReadOnly", false); }
116 ViewState ["ReadOnly"] = value;
121 [DefaultValueAttribute (true)]
122 [WebSysDescription ("")]
123 [WebCategoryAttribute ("HtmlEncode")]
124 public virtual bool HtmlEncode {
125 get { return ViewState.GetBool ("HtmlEncode", true); }
127 ViewState ["HtmlEncode"] = value;
132 [DefaultValue (true)]
133 public virtual bool HtmlEncodeFormatString {
134 get { return ViewState.GetBool ("HtmlEncodeFormatString", true); }
136 ViewState ["HtmlEncodeFormatString"] = value;
141 public override void ExtractValuesFromCell (IOrderedDictionary dictionary,
142 DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
144 bool editable = IsEditable (rowState);
146 if (cell.Controls.Count > 0) {
147 TextBox box = (TextBox) cell.Controls [0];
148 dictionary [DataField] = box.Text;
150 } else if (includeReadOnly)
151 dictionary [DataField] = cell.Text;
154 public override bool Initialize (bool enableSorting, Control control)
156 return base.Initialize (enableSorting, control);
159 public override void InitializeCell (DataControlFieldCell cell,
160 DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
162 base.InitializeCell (cell, cellType, rowState, rowIndex);
163 if (cellType == DataControlCellType.DataCell) {
164 InitializeDataCell (cell, rowState);
165 if ((rowState & DataControlRowState.Insert) == 0)
166 cell.DataBinding += new EventHandler (OnDataBindField);
170 protected virtual void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
172 bool editable = IsEditable (rowState);
174 TextBox box = new TextBox ();
175 cell.Controls.Add (box);
176 box.ToolTip = HeaderText;
180 internal bool IsEditable (DataControlRowState rowState)
182 return ((rowState & DataControlRowState.Edit) != 0 && !ReadOnly) || ((rowState & DataControlRowState.Insert) != 0 && InsertVisible);
185 protected virtual bool SupportsHtmlEncode {
189 protected virtual string FormatDataValue (object value, bool encode)
192 bool htmlEncodeFormatString = HtmlEncodeFormatString;
193 string stringValue = (value != null) ? value.ToString () : String.Empty;
194 if (value == null || (stringValue.Length == 0 && ConvertEmptyStringToNull)) {
195 if (NullDisplayText.Length == 0) {
199 res = NullDisplayText;
201 string format = DataFormatString;
202 if (!String.IsNullOrEmpty (format)) {
203 if (!encode || htmlEncodeFormatString)
204 res = String.Format (format, value);
206 res = String.Format (format, encode ? HttpUtility.HtmlEncode (stringValue) : stringValue);
211 if (encode && htmlEncodeFormatString)
212 return HttpUtility.HtmlEncode (res);
217 protected virtual object GetValue (Control controlContainer)
220 return GetDesignTimeValue ();
222 return GetBoundValue (controlContainer);
225 protected virtual object GetDesignTimeValue ()
230 object GetBoundValue (Control controlContainer)
232 object dataItem = DataBinder.GetDataItem (controlContainer);
233 if (dataItem == null)
234 throw new HttpException ("A data item was not found in the container. The container must either implement IDataItemContainer, or have a property named DataItem.");
236 if (DataField == ThisExpression)
238 else if (DataField == string.Empty)
241 return DataBinder.GetPropertyValue (dataItem, DataField);
244 protected override void LoadViewState (object state)
247 base.LoadViewState (state);
250 protected virtual void OnDataBindField (object sender, EventArgs e)
252 Control container = (Control) sender;
253 Control controlContainer = container.BindingContainer;
254 if (!(controlContainer is INamingContainer))
255 throw new HttpException ("A DataControlField must be within an INamingContainer.");
256 object val = GetValue (controlContainer);
257 TextBox box = sender as TextBox;
260 var cell = sender as DataControlFieldCell;
262 ControlCollection controls = cell.Controls;
263 int ccount = controls != null ? controls.Count : 0;
265 box = controls [0] as TextBox;
267 cell.Text = FormatDataValue (val, SupportsHtmlEncode && HtmlEncode);
274 throw new HttpException ("Bound field " + DataField + " contains a control that isn't a TextBox. Override OnDataBindField to inherit from BoundField and add different controls.");
275 if (ApplyFormatInEditMode)
276 box.Text = FormatDataValue (val, SupportsHtmlEncode && HtmlEncode);
278 box.Text = val != null ? val.ToString() : NullDisplayText;
281 protected override DataControlField CreateField ()
283 return new BoundField ();
286 protected override void CopyProperties (DataControlField newField)
288 base.CopyProperties (newField);
289 BoundField field = (BoundField) newField;
290 field.ConvertEmptyStringToNull = ConvertEmptyStringToNull;
291 field.DataField = DataField;
292 field.DataFormatString = DataFormatString;
293 field.NullDisplayText = NullDisplayText;
294 field.ReadOnly = ReadOnly;
295 field.HtmlEncode = HtmlEncode;
298 // MSDN: The ValidateSupportsCallback method is a helper method used to determine
299 // whether the controls contained in a BoundField object support callbacks.
300 // This method has been implemented as an empty method (a method that does not contain
301 // any code) to indicate that callbacks are supported.
302 public override void ValidateSupportsCallback ()