2 // System.Web.UI.WebControls.BoundField.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
7 // (C) 2005 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.
32 using System.Collections;
33 using System.Collections.Specialized;
35 using System.ComponentModel;
36 using System.Security.Permissions;
37 using System.Reflection;
39 namespace System.Web.UI.WebControls {
41 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43 public class BoundField : DataControlField
45 public static readonly string ThisExpression = "!";
47 PropertyDescriptor boundProperty;
49 [DefaultValueAttribute (false)]
50 [WebSysDescription ("")]
51 [WebCategoryAttribute ("Behavior")]
52 public virtual bool ApplyFormatInEditMode {
54 return ViewState.GetBool ("ApplyFormatInEditMode", false);
57 ViewState ["ApplyFormatInEditMode"] = value;
61 [DefaultValueAttribute (true)]
62 [WebSysDescription ("")]
63 [WebCategoryAttribute ("Behavior")]
64 public virtual bool ConvertEmptyStringToNull {
65 get { return ViewState.GetBool ("ConvertEmptyStringToNull", true); }
67 ViewState ["ConvertEmptyStringToNull"] = value;
72 [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
73 [WebSysDescription ("")]
74 [WebCategoryAttribute ("Data")]
75 [DefaultValueAttribute ("")]
76 public virtual string DataField {
77 get { return ViewState.GetString ("DataField", ""); }
79 ViewState ["DataField"] = value;
84 [DefaultValueAttribute ("")]
85 [WebSysDescription ("")]
86 [WebCategoryAttribute ("Data")]
87 public virtual string DataFormatString {
88 get { return ViewState.GetString ("DataFormatString", ""); }
90 ViewState ["DataFormatString"] = value;
95 [WebSysDescription ("")]
96 [WebCategoryAttribute ("Appearance")]
97 public override string HeaderText {
98 get { return ViewState.GetString ("HeaderText", "");
101 ViewState["HeaderText"] = value;
106 [DefaultValueAttribute ("")]
107 [WebCategoryAttribute ("Behavior")]
108 public virtual string NullDisplayText {
109 get { return ViewState.GetString ("NullDisplayText", ""); }
111 ViewState ["NullDisplayText"] = value;
116 [DefaultValueAttribute (false)]
117 [WebSysDescription ("")]
118 [WebCategoryAttribute ("Behavior")]
119 public virtual bool ReadOnly {
120 get { return ViewState.GetBool ("ReadOnly", false); }
122 ViewState ["ReadOnly"] = value;
127 [DefaultValueAttribute (true)]
128 [WebSysDescription ("")]
129 [WebCategoryAttribute ("HtmlEncode")]
130 public virtual bool HtmlEncode {
131 get { return ViewState.GetBool ("HtmlEncode", true); }
133 ViewState ["HtmlEncode"] = value;
138 public override void ExtractValuesFromCell (IOrderedDictionary dictionary,
139 DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
141 bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
142 if (editable && !ReadOnly) {
143 if (cell.Controls.Count > 0) {
144 TextBox box = (TextBox) cell.Controls [0];
145 dictionary [DataField] = box.Text;
147 } else if (includeReadOnly) {
148 dictionary [DataField] = cell.Text;
152 public override bool Initialize (bool enableSorting,
155 return base.Initialize (enableSorting, control);
158 public override void InitializeCell (DataControlFieldCell cell,
159 DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
161 base.InitializeCell (cell, cellType, rowState, rowIndex);
162 if (cellType == DataControlCellType.DataCell) {
163 InitializeDataCell (cell, rowState);
164 if ((rowState & DataControlRowState.Insert) == 0)
165 cell.DataBinding += new EventHandler (OnDataBindField);
169 protected virtual void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
171 bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
172 if (editable && !ReadOnly) {
173 TextBox box = new TextBox ();
174 box.ID = cell.ClientID;
175 cell.Controls.Add (box);
179 protected virtual bool SupportsHtmlEncode {
183 protected virtual string FormatDataValue (object value, bool encode)
186 if (value == null || (value.ToString ().Length == 0 && ConvertEmptyStringToNull)) {
187 if (NullDisplayText.Length == 0) {
192 res = NullDisplayText;
194 else if (DataFormatString.Length > 0)
195 res = string.Format (DataFormatString, value);
197 res = value.ToString ();
199 if (encode) return HttpUtility.HtmlEncode (res);
203 protected virtual object GetValue (Control controlContainer)
206 return GetDesignTimeValue ();
208 return GetBoundValue (controlContainer);
211 protected virtual object GetDesignTimeValue ()
216 object GetBoundValue (Control controlContainer)
218 object dataItem = DataBinder.GetDataItem (controlContainer);
219 if (dataItem == null)
220 throw new HttpException ("A data item was not found in the container. The container must either implement IDataItemContainer, or have a property named DataItem.");
222 if (DataField == ThisExpression)
223 return dataItem.ToString ();
224 else if (DataField == string.Empty)
227 return DataBinder.GetPropertyValue (dataItem, DataField);
230 protected virtual void OnDataBindField (object sender, EventArgs e)
232 Control cell = (Control) sender;
233 Control controlContainer = cell.BindingContainer;
234 if (!(controlContainer is INamingContainer))
235 throw new HttpException ("A DataControlField must be within an INamingContainer.");
236 object val = GetValue (controlContainer);
238 if (cell.Controls.Count > 0) {
239 TextBox box = (TextBox) cell.Controls [0];
240 if (ApplyFormatInEditMode)
241 box.Text = FormatDataValue (val, SupportsHtmlEncode && HtmlEncode);
243 box.Text = val != null ? val.ToString() : NullDisplayText;
246 ((DataControlFieldCell)cell).Text = FormatDataValue (val, SupportsHtmlEncode && HtmlEncode);
249 protected override DataControlField CreateField ()
251 return new BoundField ();
254 protected override void CopyProperties (DataControlField newField)
256 base.CopyProperties (newField);
257 BoundField field = (BoundField) newField;
258 field.ConvertEmptyStringToNull = ConvertEmptyStringToNull;
259 field.DataField = DataField;
260 field.DataFormatString = DataFormatString;
261 field.NullDisplayText = NullDisplayText;
262 field.ReadOnly = ReadOnly;
263 field.HtmlEncode = HtmlEncode;
266 // MSDN: The ValidateSupportsCallback method is a helper method used to determine
267 // whether the controls contained in a BoundField object support callbacks.
268 // This method has been implemented as an empty method (a method that does not contain
269 // any code) to indicate that callbacks are supported.
270 public override void ValidateSupportsCallback ()