b0c8a91fdf80c13106638f4cae9d883fbc933ad8
[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
31 namespace System.Web.UI.WebControls {
32         public class BoundColumn : DataGridColumn
33         {
34
35                 private string data_format_string;
36
37                 public BoundColumn ()
38                 {
39                 }
40
41                 public static readonly string thisExpr = "!";
42
43                 [DefaultValue("")]
44                 [WebSysDescription ("")]
45                 [WebCategory ("Misc")]
46                 public virtual string DataField 
47                 {
48                         get {
49                                 return ViewState.GetString ("DataField", String.Empty);
50                         }
51                         set {
52                                 ViewState ["DataField"] = value;
53                         }
54                 }
55                 
56                 [DefaultValue("")]
57                 [WebSysDescription ("")]
58                 [WebCategory ("Misc")]
59                 public virtual string DataFormatString 
60                 {
61                         get {
62                                 return ViewState.GetString ("DataFormatString", String.Empty);
63                         }
64                         set {
65                                 ViewState ["DataFormatString"] = value;
66                         }
67                 }
68
69                 [DefaultValue(false)]
70                 [WebSysDescription ("")]
71                 [WebCategory ("Misc")]
72                 public virtual bool ReadOnly 
73                 {
74                         get {
75                                 return ViewState.GetBool ("ReadOnly", false);
76                         }
77                         set {
78                                 ViewState ["ReadOnly"] = value;
79                         }
80                 }
81                 
82                 public override void Initialize ()
83                 {
84                         data_format_string = DataFormatString;
85                 }
86
87                 public override void InitializeCell (TableCell cell, int columnIndex,
88                                 ListItemType itemType)
89                 {
90                         base.InitializeCell (cell, columnIndex, itemType);
91
92                         switch (itemType) {
93                         case ListItemType.Item:
94                         case ListItemType.SelectedItem:
95                         case ListItemType.AlternatingItem:
96                                 cell.DataBinding += new EventHandler (ItemDataBinding);
97                                 break;
98                         case ListItemType.EditItem:
99                                 string df = DataField;
100                                 TextBox tb = new TextBox ();
101                                 if (df != null && df != "")
102                                         tb.DataBinding += new EventHandler (ItemDataBinding);
103                                 cell.Controls.Add (tb);
104                                 break;
105                         }
106                 }
107
108                 protected virtual string FormatDataValue (object dataValue)
109                 {
110                         if (dataValue == null)
111                                 return "";
112
113                         if (data_format_string == String.Empty)
114                                 return dataValue.ToString ();
115
116                         return String.Format (data_format_string, dataValue);
117                 }
118
119                 string GetValueFromItem (DataGridItem item)
120                 {
121                         object val;
122                         if (DataField != thisExpr) {
123                                 val = DataBinder.Eval (item.DataItem, DataField);
124                         } else {
125                                 val = item.DataItem;
126                         }
127
128                         string text = FormatDataValue (val);
129                         return (text != "" ?  text : "&nbsp;");
130                 }
131
132                 void ItemDataBinding (object sender, EventArgs e)
133                 {
134                         Control ctrl = (Control) sender;
135                         string text = GetValueFromItem ((DataGridItem) ctrl.NamingContainer);
136
137                         TableCell cell = sender as TableCell;
138                         if (cell == null) {
139                                 TextBox tb = (TextBox) sender;
140                                 tb.Text = text;
141                         } else {
142                                 cell.Text = text;
143                         }
144                 }
145         }
146 }
147