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 bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
136 if (editable && !ReadOnly) {
137 if (cell.Controls.Count > 0) {
138 TextBox box = (TextBox) cell.Controls [0];
139 dictionary [DataField] = box.Text;
141 } else if (includeReadOnly) {
142 dictionary [DataField] = cell.Text;
146 public override void InitializeCell (DataControlFieldCell cell,
147 DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
149 base.InitializeCell (cell, cellType, rowState, rowIndex);
150 if (cellType == DataControlCellType.DataCell) {
151 InitializeDataCell (cell, rowState);
152 if ((rowState & DataControlRowState.Insert) == 0)
153 cell.DataBinding += new EventHandler (OnDataBindField);
157 public virtual void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
159 bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
160 if (editable && !ReadOnly) {
161 TextBox box = new TextBox ();
162 cell.Controls.Add (box);
166 protected virtual bool SupportsHtmlEncode {
170 protected virtual string FormatDataValue (object value, bool encode)
173 if (value == null || (value.ToString().Length == 0 && ConvertEmptyStringToNull))
174 res = NullDisplayText;
175 else if (DataFormatString.Length > 0)
176 res = string.Format (DataFormatString, value);
178 res = value.ToString ();
180 if (encode) return HttpUtility.HtmlEncode (res);
184 protected virtual object GetValue (Control controlContainer)
187 return GetDesignTimeValue ();
189 return GetBoundValue (controlContainer);
192 protected virtual object GetDesignTimeValue ()
194 return GetBoundValue (Control);
197 object GetBoundValue (Control controlContainer)
199 if (DataField == ThisExpression)
200 return controlContainer.ToString ();
202 IDataItemContainer dic = (IDataItemContainer) controlContainer;
203 if (boundProperty == null) {
204 boundProperty = TypeDescriptor.GetProperties (dic.DataItem) [DataField];
205 if (boundProperty == null)
206 new InvalidOperationException ("Property '" + DataField + "' not found in object of type " + dic.DataItem.GetType());
208 return boundProperty.GetValue (dic.DataItem);
212 protected virtual void OnDataBindField (object sender, EventArgs e)
214 DataControlFieldCell cell = (DataControlFieldCell) sender;
215 if (cell.Controls.Count > 0) {
216 TextBox box = (TextBox) cell.Controls [0];
217 object val = GetValue (cell.BindingContainer);
218 box.Text = val != null ? val.ToString() : "";
221 cell.Text = FormatDataValue (GetValue (cell.BindingContainer), SupportsHtmlEncode && HtmlEncode);
224 protected override DataControlField CreateField ()
226 return new BoundField ();
229 protected override void CopyProperties (DataControlField newField)
231 base.CopyProperties (newField);
232 BoundField field = (BoundField) newField;
233 field.ConvertEmptyStringToNull = ConvertEmptyStringToNull;
234 field.DataField = DataField;
235 field.DataFormatString = DataFormatString;
236 field.NullDisplayText = NullDisplayText;
237 field.ReadOnly = ReadOnly;
238 field.HtmlEncode = HtmlEncode;