2008-03-13 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / ButtonField.cs
1 //
2 // System.Web.UI.WebControls.ButtonField.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.Reflection;
33 using System.Collections;
34 using System.Collections.Specialized;
35 using System.Web.UI;
36 using System.ComponentModel;
37 using System.Security.Permissions;
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 ButtonField : ButtonFieldBase
44         {
45                 PropertyDescriptor boundProperty;
46
47                 [DefaultValueAttribute ("")]
48                 [WebSysDescription ("Raised when a Button Command is executed.")]
49                 [WebCategoryAttribute ("Behavior")]
50                 public virtual string CommandName {
51                         get { return ViewState.GetString ("CommandName", ""); }
52                         set {
53                                 ViewState ["CommandName"] = value;
54                                 OnFieldChanged ();
55                         }
56                 }
57                 
58                 [DefaultValueAttribute ("")]
59                 [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
60                 [WebSysDescription ("")]
61                 [WebCategoryAttribute ("Data")]
62                 public virtual string DataTextField {
63                         get { return ViewState.GetString ("DataTextField", ""); }
64                         set {
65                                 ViewState ["DataTextField"] = value;
66                                 OnFieldChanged ();
67                         }
68                 }
69                 
70                 [DefaultValueAttribute ("")]
71                 [WebSysDescription ("")]
72                 [WebCategoryAttribute ("Data")]
73                 public virtual string DataTextFormatString {
74                         get { return ViewState.GetString ("DataTextFormatString", ""); }
75                         set {
76                                 ViewState ["DataTextFormatString"] = value;
77                                 OnFieldChanged ();
78                         }
79                 }
80                 
81                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
82                 [WebSysDescription ("")]
83                 [WebCategoryAttribute ("Appearance")]
84                 [DefaultValueAttribute ("")]
85                 [UrlPropertyAttribute]
86                 public virtual string ImageUrl {
87                         get { return ViewState.GetString ("ImageUrl", ""); }
88                         set {
89                                 ViewState ["ImageUrl"] = value;
90                                 OnFieldChanged ();
91                         }
92                 }
93                 
94                 [LocalizableAttribute (true)]
95                 [DefaultValueAttribute ("")]
96                 [WebSysDescription ("")]
97                 [WebCategoryAttribute ("Appearance")]
98                 public virtual string Text {
99                         get { return ViewState.GetString ("Text", ""); }
100                         set {
101                                 ViewState ["Text"] = value;
102                                 OnFieldChanged ();
103                         }
104                 }
105                 
106                 public override bool Initialize (bool sortingEnabled, Control control)
107                 {
108                         return base.Initialize (sortingEnabled, control);
109                 }
110                 
111                 protected virtual string FormatDataTextValue (object value)
112                 {
113                         if (DataTextFormatString.Length > 0)
114                                 return string.Format (DataTextFormatString, value);
115                         else if (value == null)
116                                 return string.Empty;
117                         else
118                                 return value.ToString ();
119                 }
120                 
121                 public override void InitializeCell (DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
122                 {
123                         string index = rowIndex.ToString ();
124                         
125                         if (cellType == DataControlCellType.DataCell) {
126                                 
127                                 IDataControlButton btn = DataControlButton.CreateButton (ButtonType, Control, Text, ImageUrl, CommandName, index, false);
128
129                                 if (CausesValidation) {
130                                         btn.Container = null;
131                                         btn.CausesValidation = true;
132                                         btn.ValidationGroup = ValidationGroup;
133                                 }
134                                 
135                                 if (DataTextField != "") {
136                                         if ((rowState & DataControlRowState.Insert) == 0)
137                                                 cell.DataBinding += new EventHandler (OnDataBindField);
138                                 }
139                                 cell.Controls.Add ((Control) btn);
140                         }
141                         else
142                                 base.InitializeCell (cell, cellType, rowState, rowIndex);
143                 }
144                 
145                 void OnDataBindField (object sender, EventArgs e)
146                 {
147                         DataControlFieldCell cell = (DataControlFieldCell) sender;
148                         IDataControlButton btn = (IDataControlButton) cell.Controls [0]; 
149                         btn.Text = FormatDataTextValue (GetBoundValue (cell.BindingContainer));
150                 }
151                 
152                 object GetBoundValue (Control controlContainer)
153                 {
154                         IDataItemContainer dic = controlContainer as IDataItemContainer;
155                         if (boundProperty == null) {
156                                 boundProperty = TypeDescriptor.GetProperties (dic.DataItem) [DataTextField];
157                                 if (boundProperty == null)
158                                         new InvalidOperationException ("Property '" + DataTextField + "' not found in object of type " + dic.DataItem.GetType());
159                         }
160                         return boundProperty.GetValue (dic.DataItem);
161                 }
162                 
163                 protected override DataControlField CreateField ()
164                 {
165                         return new ButtonField ();
166                 }
167                 
168                 protected override void CopyProperties (DataControlField newField)
169                 {
170                         base.CopyProperties (newField);
171                         ButtonField field = (ButtonField) newField;
172                         field.CommandName = CommandName;
173                         field.DataTextField = DataTextField;
174                         field.DataTextFormatString = DataTextFormatString;
175                         field.ImageUrl = ImageUrl;
176                         field.Text = Text;
177                 }
178
179                 // MSDN: The ValidateSupportsCallback method is a helper method used to determine 
180                 // whether the controls contained in a BoundField object support callbacks. 
181                 // This method has been implemented as an empty method (a method that does not 
182                 // contain any code) to indicate that callbacks are supported.
183                 public override void ValidateSupportsCallback ()
184                 {
185                 }
186         }
187 }
188 #endif