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 = IsEditable (rowState);
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 = IsEditable (rowState);
173 TextBox box = new TextBox ();
174 cell.Controls.Add (box);
175 box.ToolTip = HeaderText;
179 internal bool IsEditable (DataControlRowState rowState)
181 return ((rowState & DataControlRowState.Edit) != 0 && !ReadOnly) || ((rowState & DataControlRowState.Insert) != 0 && InsertVisible);
184 protected virtual bool SupportsHtmlEncode {
188 protected virtual string FormatDataValue (object value, bool encode)
191 string stringValue = (value != null) ? value.ToString () : string.Empty;
192 if (value == null || (stringValue.Length == 0 && ConvertEmptyStringToNull)) {
193 if (NullDisplayText.Length == 0) {
198 res = NullDisplayText;
200 else if (DataFormatString.Length > 0)
201 res = string.Format (DataFormatString, value);
205 if (encode) return HttpUtility.HtmlEncode (res);
209 protected virtual object GetValue (Control controlContainer)
212 return GetDesignTimeValue ();
214 return GetBoundValue (controlContainer);
217 protected virtual object GetDesignTimeValue ()
222 object GetBoundValue (Control controlContainer)
224 object dataItem = DataBinder.GetDataItem (controlContainer);
225 if (dataItem == null)
226 throw new HttpException ("A data item was not found in the container. The container must either implement IDataItemContainer, or have a property named DataItem.");
228 if (DataField == ThisExpression)
230 else if (DataField == string.Empty)
233 return DataBinder.GetPropertyValue (dataItem, DataField);
236 protected virtual void OnDataBindField (object sender, EventArgs e)
238 Control cell = (Control) sender;
239 Control controlContainer = cell.BindingContainer;
240 if (!(controlContainer is INamingContainer))
241 throw new HttpException ("A DataControlField must be within an INamingContainer.");
242 object val = GetValue (controlContainer);
244 if (cell.Controls.Count > 0) {
245 TextBox box = (TextBox) cell.Controls [0];
246 if (ApplyFormatInEditMode)
247 box.Text = FormatDataValue (val, SupportsHtmlEncode && HtmlEncode);
249 box.Text = val != null ? val.ToString() : NullDisplayText;
252 ((DataControlFieldCell)cell).Text = FormatDataValue (val, SupportsHtmlEncode && HtmlEncode);
255 protected override DataControlField CreateField ()
257 return new BoundField ();
260 protected override void CopyProperties (DataControlField newField)
262 base.CopyProperties (newField);
263 BoundField field = (BoundField) newField;
264 field.ConvertEmptyStringToNull = ConvertEmptyStringToNull;
265 field.DataField = DataField;
266 field.DataFormatString = DataFormatString;
267 field.NullDisplayText = NullDisplayText;
268 field.ReadOnly = ReadOnly;
269 field.HtmlEncode = HtmlEncode;
272 // MSDN: The ValidateSupportsCallback method is a helper method used to determine
273 // whether the controls contained in a BoundField object support callbacks.
274 // This method has been implemented as an empty method (a method that does not contain
275 // any code) to indicate that callbacks are supported.
276 public override void ValidateSupportsCallback ()