2005-04-08 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                         if ((rowState & DataControlRowState.Edit) != 0 && !ReadOnly) {
136                                 if (cell.Controls.Count > 0) {
137                                         TextBox box = (TextBox) cell.Controls [0];
138                                         dictionary [DataField] = box.Text;
139                                 }
140                         } else if (includeReadOnly) {
141                                 dictionary [DataField] = cell.Text;
142                         }
143                 }
144                 
145                 public override void InitializeCell (DataControlFieldCell cell,
146                         DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
147                 {
148                         base.InitializeCell (cell, cellType, rowState, rowIndex);
149                         if (cellType == DataControlCellType.DataCell) {
150                                 InitializeDataCell (cell, rowState);
151                                 cell.DataBinding += new EventHandler (OnDataBindField);
152                         }
153                 }
154                 
155                 public virtual void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
156                 {
157                         if ((rowState & DataControlRowState.Edit) != 0 && !ReadOnly) {
158                                 TextBox box = new TextBox ();
159                                 cell.Controls.Add (box);
160                         }
161                 }
162                 
163                 protected virtual bool SupportsHtmlEncode {
164                         get { return true; }
165                 }
166                 
167                 protected virtual string FormatDataValue (object value, bool encode)
168                 {
169                         string res;
170                         if (value == null || (value.ToString().Length == 0 && ConvertEmptyStringToNull))
171                                 res = NullDisplayText;
172                         else if (DataFormatString.Length > 0)
173                                 res = string.Format (DataFormatString, value);
174                         else
175                                 res = value.ToString ();
176                                 
177                         if (encode) return HttpUtility.HtmlEncode (res);
178                         else return res;
179                 }
180                 
181                 protected virtual object GetValue (Control controlContainer)
182                 {
183                         if (DesignMode)
184                                 return GetDesignTimeValue ();
185                         else
186                                 return GetBoundValue (controlContainer);
187                 }
188                 
189                 protected virtual object GetDesignTimeValue ()
190                 {
191                         return GetBoundValue (Control);
192                 }
193                 
194                 object GetBoundValue (Control controlContainer)
195                 {
196                         if (DataField == ThisExpression)
197                                 return controlContainer.ToString ();
198                         else {
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());
204                                 }
205                                 return boundProperty.GetValue (dic.DataItem);
206                         }
207                 }
208                 
209                 protected virtual void OnDataBindField (object sender, EventArgs e)
210                 {
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() : "";
216                         }
217                         else
218                                 cell.Text = FormatDataValue (GetValue (cell.BindingContainer), SupportsHtmlEncode && HtmlEncode);
219                 }
220         }
221 }
222 #endif