merge -r 61110:61111
[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 ("")]\r
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 ("")]\r
85                 [UrlPropertyAttribute]\r
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                                 DataControlButton btn = new DataControlButton (Control);
127                                 btn.CommandName = CommandName;
128                                 btn.CommandArgument = index;
129                                 
130                                 if (DataTextField != "") {
131                                         if ((rowState & DataControlRowState.Insert) == 0)
132                                                 cell.DataBinding += new EventHandler (OnDataBindField);
133                                 }
134                                 else {
135                                         btn.Text = Text;
136                                         btn.ButtonType = ButtonType;
137                                         if (ButtonType == ButtonType.Image) btn.ImageUrl = ImageUrl;
138                                 }
139                                 cell.Controls.Add (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                         DataControlButton btn = (DataControlButton) cell.Controls [0]; 
149                         btn.Text = FormatDataTextValue (GetBoundValue (cell.BindingContainer));
150                         if (ButtonType == ButtonType.Image) btn.ImageUrl = ImageUrl;
151                         btn.ButtonType = ButtonType;
152                 }
153                 
154                 object GetBoundValue (Control controlContainer)
155                 {
156                         IDataItemContainer dic = controlContainer as IDataItemContainer;
157                         if (boundProperty == null) {
158                                 boundProperty = TypeDescriptor.GetProperties (dic.DataItem) [DataTextField];
159                                 if (boundProperty == null)
160                                         new InvalidOperationException ("Property '" + DataTextField + "' not found in object of type " + dic.DataItem.GetType());
161                         }
162                         return boundProperty.GetValue (dic.DataItem);
163                 }
164                 
165                 protected override DataControlField CreateField ()
166                 {
167                         return new ButtonField ();
168                 }
169                 
170                 protected override void CopyProperties (DataControlField newField)
171                 {
172                         base.CopyProperties (newField);
173                         ButtonField field = (ButtonField) newField;
174                         field.CommandName = CommandName;
175                         field.DataTextField = DataTextField;
176                         field.DataTextFormatString = DataTextFormatString;
177                         field.ImageUrl = ImageUrl;
178                         field.Text = Text;
179                 }
180
181                 [MonoTODO]
182                 public override void ValidateSupportsCallback ()
183                 {
184                         throw new NotImplementedException ();
185                 }
186         }
187 }
188 #endif