2005-04-21 Lluis Sanchez Gual <lluis@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / BoundField.cs
1 //
2 // System.Web.UI.WebControls.BoundField.cs
3 //
4 // Authors:
5 //      Lluis Sanchez Gual (lluis@novell.com)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 #if NET_2_0
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.Web.UI;
35 using System.ComponentModel;
36 using System.Security.Permissions;
37 using System.Reflection;
38
39 namespace System.Web.UI.WebControls {
40
41         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43         public class BoundField : DataControlField
44         {
45                 public static readonly string ThisExpression = "!";
46                 
47                 PropertyDescriptor boundProperty;
48                 
49                 [DefaultValueAttribute (true)]
50                 [WebCategoryAttribute ("Behavior")]
51                 public virtual bool ConvertEmptyStringToNull {
52                         get {
53                                 object ob = ViewState ["ConvertEmptyStringToNull"];
54                                 if (ob != null) return (bool) ob;
55                                 return true;
56                         }
57                         set {
58                                 ViewState ["ConvertEmptyStringToNull"] = value;
59                                 OnFieldChanged ();
60                         }
61                 }
62
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 {
67                         get {
68                                 object ob = ViewState ["DataField"];
69                                 if (ob != null) return (string) ob;
70                                 return "";
71                         }
72                         set {
73                                 ViewState ["DataField"] = value;
74                                 OnFieldChanged ();
75                         }
76                 }
77
78                 [DefaultValueAttribute ("")]
79                 [WebCategoryAttribute ("Data")]
80                 public virtual string DataFormatString {
81                         get {
82                                 object ob = ViewState ["DataFormatString"];
83                                 if (ob != null) return (string) ob;
84                                 return "";
85                         }
86                         set {
87                                 ViewState ["DataFormatString"] = value;
88                                 OnFieldChanged ();
89                         }
90                 }
91
92                 [DefaultValueAttribute ("")]
93                 [WebCategoryAttribute ("Behavior")]
94                 public virtual string NullDisplayText {
95                         get {
96                                 object ob = ViewState ["NullDisplayText"];
97                                 if (ob != null) return (string) ob;
98                                 return "";
99                         }
100                         set {
101                                 ViewState ["NullDisplayText"] = value;
102                                 OnFieldChanged ();
103                         }
104                 }
105
106                 [WebCategoryAttribute ("Behavior")]
107                 [DefaultValueAttribute (false)]
108                 public bool ReadOnly {
109                         get {
110                                 object val = ViewState ["ReadOnly"];
111                                 return val != null ? (bool) val : false;
112                         }
113                         set { 
114                                 ViewState ["ReadOnly"] = value;
115                                 OnFieldChanged ();
116                         }
117                 }
118
119                 [WebCategoryAttribute ("HtmlEncode")]
120                 [DefaultValueAttribute (true)]
121                 public virtual bool HtmlEncode {
122                         get {
123                                 object val = ViewState ["HtmlEncode"];
124                                 return val != null ? (bool) val : true;
125                         }
126                         set { 
127                                 ViewState ["HtmlEncode"] = true;
128                                 OnFieldChanged ();
129                         }
130                 }
131                 
132                 public override void ExtractValuesFromCell (IOrderedDictionary dictionary,
133                         DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
134                 {
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;
140                                 }
141                         } else if (includeReadOnly) {
142                                 dictionary [DataField] = cell.Text;
143                         }
144                 }
145                 
146                 public override void InitializeCell (DataControlFieldCell cell,
147                         DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
148                 {
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);
154                         }
155                 }
156                 
157                 public virtual void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
158                 {
159                         bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
160                         if (editable && !ReadOnly) {
161                                 TextBox box = new TextBox ();
162                                 cell.Controls.Add (box);
163                         }
164                 }
165                 
166                 protected virtual bool SupportsHtmlEncode {
167                         get { return true; }
168                 }
169                 
170                 protected virtual string FormatDataValue (object value, bool encode)
171                 {
172                         string res;
173                         if (value == null || (value.ToString().Length == 0 && ConvertEmptyStringToNull))
174                                 res = NullDisplayText;
175                         else if (DataFormatString.Length > 0)
176                                 res = string.Format (DataFormatString, value);
177                         else
178                                 res = value.ToString ();
179                                 
180                         if (encode) return HttpUtility.HtmlEncode (res);
181                         else return res;
182                 }
183                 
184                 protected virtual object GetValue (Control controlContainer)
185                 {
186                         if (DesignMode)
187                                 return GetDesignTimeValue ();
188                         else
189                                 return GetBoundValue (controlContainer);
190                 }
191                 
192                 protected virtual object GetDesignTimeValue ()
193                 {
194                         return GetBoundValue (Control);
195                 }
196                 
197                 object GetBoundValue (Control controlContainer)
198                 {
199                         if (DataField == ThisExpression)
200                                 return controlContainer.ToString ();
201                         else {
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());
207                                 }
208                                 return boundProperty.GetValue (dic.DataItem);
209                         }
210                 }
211                 
212                 protected virtual void OnDataBindField (object sender, EventArgs e)
213                 {
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() : "";
219                         }
220                         else
221                                 cell.Text = FormatDataValue (GetValue (cell.BindingContainer), SupportsHtmlEncode && HtmlEncode);
222                 }
223                 
224                 protected override DataControlField CreateField ()
225                 {
226                         return new BoundField ();
227                 }
228                 
229                 protected override void CopyProperties (DataControlField newField)
230                 {
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;
239                 }
240         }
241 }
242 #endif