2009-10-30 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / BoundColumn.cs
1 //
2 // System.Web.UI.WebControls.BoundColumn.cs
3 //
4 // Author:
5 //      Dick Porter  <dick@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System.ComponentModel;
30 using System.Security.Permissions;
31
32 namespace System.Web.UI.WebControls {
33
34         // CAS
35         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
36         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37         public class BoundColumn : DataGridColumn 
38         {
39                 string data_format_string;
40
41                 public BoundColumn ()
42                 {
43                 }
44
45                 public static readonly string thisExpr = "!";
46
47                 [DefaultValue("")]
48                 [WebSysDescription ("")]
49                 [WebCategory ("Misc")]
50                 public virtual string DataField 
51                 {
52                         get {
53                                 return ViewState.GetString ("DataField", String.Empty);
54                         }
55                         set {
56                                 ViewState ["DataField"] = value;
57                         }
58                 }
59                 
60                 [DefaultValue("")]
61                 [WebSysDescription ("")]
62                 [WebCategory ("Misc")]
63                 public virtual string DataFormatString 
64                 {
65                         get {
66                                 return ViewState.GetString ("DataFormatString", String.Empty);
67                         }
68                         set {
69                                 ViewState ["DataFormatString"] = value;
70                         }
71                 }
72
73                 [DefaultValue(false)]
74                 [WebSysDescription ("")]
75                 [WebCategory ("Misc")]
76                 public virtual bool ReadOnly 
77                 {
78                         get {
79                                 return ViewState.GetBool ("ReadOnly", false);
80                         }
81                         set {
82                                 ViewState ["ReadOnly"] = value;
83                         }
84                 }
85                 
86                 public override void Initialize ()
87                 {
88                         data_format_string = DataFormatString;
89                 }
90
91                 public override void InitializeCell (TableCell cell, int columnIndex,
92                                 ListItemType itemType)
93                 {
94                         base.InitializeCell (cell, columnIndex, itemType);
95
96                         string df = DataField;
97
98                         switch (itemType) {
99                         case ListItemType.Item:
100                         case ListItemType.SelectedItem:
101                         case ListItemType.AlternatingItem:
102                                 if (df != null && df.Length != 0)
103                                         cell.DataBinding += new EventHandler (ItemDataBinding);
104                                 break;
105                         case ListItemType.EditItem:
106                                 if (ReadOnly && df != null && df.Length != 0) {
107                                         cell.DataBinding += new EventHandler (ItemDataBinding);
108                                         break;
109                                 }
110                                 TextBox tb = new TextBox ();
111                                 if (df != null && df.Length != 0)
112                                         tb.DataBinding += new EventHandler (ItemDataBinding);
113                                 cell.Controls.Add (tb);
114                                 break;
115                         }
116                 }
117
118                 protected virtual string FormatDataValue (object dataValue)
119                 {
120                         if (dataValue == null)
121                                 return "";
122
123                         if (data_format_string == String.Empty)
124                                 return dataValue.ToString ();
125
126                         return String.Format (data_format_string, dataValue);
127                 }
128
129                 string GetValueFromItem (DataGridItem item)
130                 {
131                         object val;
132                         if (DataField != thisExpr) {
133                                 val = DataBinder.Eval (item.DataItem, DataField);
134                         } else {
135                                 val = item.DataItem;
136                         }
137
138                         string text = FormatDataValue (val);
139                         return (text != "" ?  text : "&nbsp;");
140                 }
141
142                 void ItemDataBinding (object sender, EventArgs e)
143                 {
144                         Control ctrl = (Control) sender;
145                         string text = GetValueFromItem ((DataGridItem) ctrl.NamingContainer);
146
147                         TableCell cell = sender as TableCell;
148                         if (cell == null) {
149                                 TextBox tb = (TextBox) sender;
150                                 tb.Text = text;
151                         } else {
152                                 cell.Text = text;
153                         }
154                 }
155         }
156 }
157