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;
50 [DefaultValueAttribute (false)]
51 [WebSysDescription ("")]
52 [WebCategoryAttribute ("Behavior")]
53 public virtual bool ApplyFormatInEditMode {
55 throw new NotImplementedException ();
58 throw new NotImplementedException ();
62 [DefaultValueAttribute (true)]
63 [WebSysDescription ("")]
64 [WebCategoryAttribute ("Behavior")]
65 public virtual bool ConvertEmptyStringToNull {
67 object ob = ViewState ["ConvertEmptyStringToNull"];
68 if (ob != null) return (bool) ob;
72 ViewState ["ConvertEmptyStringToNull"] = value;
77 [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
78 [WebSysDescription ("")]
79 [WebCategoryAttribute ("Data")]
80 [DefaultValueAttribute ("")]
81 public virtual string DataField {
83 object ob = ViewState ["DataField"];
84 if (ob != null) return (string) ob;
88 ViewState ["DataField"] = value;
93 [DefaultValueAttribute ("")]
94 [WebSysDescription ("")]
95 [WebCategoryAttribute ("Data")]
96 public virtual string DataFormatString {
98 object ob = ViewState ["DataFormatString"];
99 if (ob != null) return (string) ob;
103 ViewState ["DataFormatString"] = value;
109 [WebSysDescription ("")]
110 [WebCategoryAttribute ("Appearance")]
111 public virtual string HeaderText {
113 throw new NotImplementedException ();
116 throw new NotImplementedException ();
120 [DefaultValueAttribute ("")]
121 [WebCategoryAttribute ("Behavior")]
122 public virtual string NullDisplayText {
124 object ob = ViewState ["NullDisplayText"];
125 if (ob != null) return (string) ob;
129 ViewState ["NullDisplayText"] = value;
134 [DefaultValueAttribute (false)]
135 [WebSysDescription ("")]
136 [WebCategoryAttribute ("Behavior")]
137 public bool ReadOnly {
139 object val = ViewState ["ReadOnly"];
140 return val != null ? (bool) val : false;
143 ViewState ["ReadOnly"] = value;
148 [DefaultValueAttribute (true)]
149 [WebSysDescription ("")]
150 [WebCategoryAttribute ("HtmlEncode")]
151 public virtual bool HtmlEncode {
153 object val = ViewState ["HtmlEncode"];
154 return val != null ? (bool) val : true;
157 ViewState ["HtmlEncode"] = true;
162 public override void ExtractValuesFromCell (IOrderedDictionary dictionary,
163 DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
165 bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
166 if (editable && !ReadOnly) {
167 if (cell.Controls.Count > 0) {
168 TextBox box = (TextBox) cell.Controls [0];
169 dictionary [DataField] = box.Text;
171 } else if (includeReadOnly) {
172 dictionary [DataField] = cell.Text;
177 public override bool Initialize (bool enableSorting,
180 return base.Initialize (enableSorting, control);
183 public override void InitializeCell (DataControlFieldCell cell,
184 DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
186 base.InitializeCell (cell, cellType, rowState, rowIndex);
187 if (cellType == DataControlCellType.DataCell) {
188 InitializeDataCell (cell, rowState);
189 if ((rowState & DataControlRowState.Insert) == 0)
190 cell.DataBinding += new EventHandler (OnDataBindField);
194 public virtual void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
196 bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
197 if (editable && !ReadOnly) {
198 TextBox box = new TextBox ();
199 cell.Controls.Add (box);
203 protected virtual bool SupportsHtmlEncode {
207 protected virtual string FormatDataValue (object value, bool encode)
210 if (value == null || (value.ToString().Length == 0 && ConvertEmptyStringToNull))
211 res = NullDisplayText;
212 else if (DataFormatString.Length > 0)
213 res = string.Format (DataFormatString, value);
215 res = value.ToString ();
217 if (encode) return HttpUtility.HtmlEncode (res);
221 protected virtual object GetValue (Control controlContainer)
224 return GetDesignTimeValue ();
226 return GetBoundValue (controlContainer);
229 protected virtual object GetDesignTimeValue ()
231 return GetBoundValue (Control);
234 object GetBoundValue (Control controlContainer)
236 if (DataField == ThisExpression)
237 return controlContainer.ToString ();
239 IDataItemContainer dic = (IDataItemContainer) controlContainer;
240 if (boundProperty == null) {
241 boundProperty = TypeDescriptor.GetProperties (dic.DataItem) [DataField];
242 if (boundProperty == null)
243 new InvalidOperationException ("Property '" + DataField + "' not found in object of type " + dic.DataItem.GetType());
245 return boundProperty.GetValue (dic.DataItem);
249 protected virtual void OnDataBindField (object sender, EventArgs e)
251 DataControlFieldCell cell = (DataControlFieldCell) sender;
252 if (cell.Controls.Count > 0) {
253 TextBox box = (TextBox) cell.Controls [0];
254 object val = GetValue (cell.BindingContainer);
255 box.Text = val != null ? val.ToString() : "";
258 cell.Text = FormatDataValue (GetValue (cell.BindingContainer), SupportsHtmlEncode && HtmlEncode);
261 protected override DataControlField CreateField ()
263 return new BoundField ();
266 protected override void CopyProperties (DataControlField newField)
268 base.CopyProperties (newField);
269 BoundField field = (BoundField) newField;
270 field.ConvertEmptyStringToNull = ConvertEmptyStringToNull;
271 field.DataField = DataField;
272 field.DataFormatString = DataFormatString;
273 field.NullDisplayText = NullDisplayText;
274 field.ReadOnly = ReadOnly;
275 field.HtmlEncode = HtmlEncode;
279 public override void ValidateSupportsCallback ()
281 throw new NotImplementedException ();