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 (true)]
50 [WebCategoryAttribute ("Behavior")]
51 public virtual bool ConvertEmptyStringToNull {
53 object ob = ViewState ["ConvertEmptyStringToNull"];
54 if (ob != null) return (bool) ob;
58 ViewState ["ConvertEmptyStringToNull"] = value;
63 [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
64 [WebCategoryAttribute ("Data")]
65 [DefaultValueAttribute ("")]
66 public virtual string DataField {
68 object ob = ViewState ["DataField"];
69 if (ob != null) return (string) ob;
73 ViewState ["DataField"] = value;
78 [DefaultValueAttribute ("")]
79 [WebCategoryAttribute ("Data")]
80 public virtual string DataFormatString {
82 object ob = ViewState ["DataFormatString"];
83 if (ob != null) return (string) ob;
87 ViewState ["DataFormatString"] = value;
92 [DefaultValueAttribute ("")]
93 [WebCategoryAttribute ("Behavior")]
94 public virtual string NullDisplayText {
96 object ob = ViewState ["NullDisplayText"];
97 if (ob != null) return (string) ob;
101 ViewState ["NullDisplayText"] = value;
106 [WebCategoryAttribute ("Behavior")]
107 [DefaultValueAttribute (false)]
108 public bool ReadOnly {
110 object val = ViewState ["ReadOnly"];
111 return val != null ? (bool) val : false;
114 ViewState ["ReadOnly"] = value;
119 [WebCategoryAttribute ("HtmlEncode")]
120 [DefaultValueAttribute (true)]
121 public virtual bool HtmlEncode {
123 object val = ViewState ["HtmlEncode"];
124 return val != null ? (bool) val : true;
127 ViewState ["HtmlEncode"] = true;
132 public override void ExtractValuesFromCell (IOrderedDictionary dictionary,
133 DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
135 if ((rowState & DataControlRowState.Edit) != 0 && !ReadOnly) {
136 if (cell.Controls.Count > 0) {
137 TextBox box = (TextBox) cell.Controls [0];
138 dictionary [DataField] = box.Text;
140 } else if (includeReadOnly) {
141 dictionary [DataField] = cell.Text;
145 public override void InitializeCell (DataControlFieldCell cell,
146 DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
148 base.InitializeCell (cell, cellType, rowState, rowIndex);
149 if (cellType == DataControlCellType.DataCell) {
150 InitializeDataCell (cell, rowState);
151 cell.DataBinding += new EventHandler (OnDataBindField);
155 public virtual void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
157 if ((rowState & DataControlRowState.Edit) != 0 && !ReadOnly) {
158 TextBox box = new TextBox ();
159 cell.Controls.Add (box);
163 protected virtual bool SupportsHtmlEncode {
167 protected virtual string FormatDataValue (object value, bool encode)
170 if (value == null || (value.ToString().Length == 0 && ConvertEmptyStringToNull))
171 res = NullDisplayText;
172 else if (DataFormatString.Length > 0)
173 res = string.Format (DataFormatString, value);
175 res = value.ToString ();
177 if (encode) return HttpUtility.HtmlEncode (res);
181 protected virtual object GetValue (Control controlContainer)
184 return GetDesignTimeValue ();
186 return GetBoundValue (controlContainer);
189 protected virtual object GetDesignTimeValue ()
191 return GetBoundValue (Control);
194 object GetBoundValue (Control controlContainer)
196 if (DataField == ThisExpression)
197 return controlContainer.ToString ();
199 IDataItemContainer dic = (IDataItemContainer) controlContainer;
200 if (boundProperty == null) {
201 boundProperty = TypeDescriptor.GetProperties (dic.DataItem) [DataField];
202 if (boundProperty == null)
203 new InvalidOperationException ("Property '" + DataField + "' not found in object of type " + dic.DataItem.GetType());
205 return boundProperty.GetValue (dic.DataItem);
209 protected virtual void OnDataBindField (object sender, EventArgs e)
211 DataControlFieldCell cell = (DataControlFieldCell) sender;
212 if (cell.Controls.Count > 0) {
213 TextBox box = (TextBox) cell.Controls [0];
214 object val = GetValue (cell.BindingContainer);
215 box.Text = val != null ? val.ToString() : "";
218 cell.Text = FormatDataValue (GetValue (cell.BindingContainer), SupportsHtmlEncode && HtmlEncode);