2005-10-04 Gonzalo Paniagua Javier <gonzalo@ximian.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 {
52                                 object ob = ViewState ["CommandName"];
53                                 if (ob != null) return (string) ob;
54                                 return "";
55                         }
56                         set {
57                                 ViewState ["CommandName"] = value;
58                                 OnFieldChanged ();
59                         }
60                 }
61                 
62                 [DefaultValueAttribute ("")]
63                 [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
64                 [WebSysDescription ("")]
65                 [WebCategoryAttribute ("Data")]
66                 public virtual string DataTextField {
67                         get {
68                                 object ob = ViewState ["DataTextField"];
69                                 if (ob != null) return (string) ob;
70                                 return "";
71                         }
72                         set {
73                                 ViewState ["DataTextField"] = value;
74                                 OnFieldChanged ();
75                         }
76                 }
77                 
78                 [DefaultValueAttribute ("")]\r
79                 [WebSysDescription ("")]
80                 [WebCategoryAttribute ("Data")]
81                 public virtual string DataTextFormatString {
82                         get {
83                                 object ob = ViewState ["DataTextFormatString"];
84                                 if (ob != null) return (string) ob;
85                                 return "";
86                         }
87                         set {
88                                 ViewState ["DataTextFormatString"] = value;
89                                 OnFieldChanged ();
90                         }
91                 }
92                 
93                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
94                 [WebSysDescription ("")]
95                 [WebCategoryAttribute ("Appearance")]
96                 [DefaultValueAttribute ("")]\r
97                 [UrlPropertyAttribute]\r
98                 public virtual string ImageUrl {
99                         get {
100                                 object ob = ViewState ["ImageUrl"];
101                                 if (ob != null) return (string) ob;
102                                 return "";
103                         }
104                         set {
105                                 ViewState ["ImageUrl"] = value;
106                                 OnFieldChanged ();
107                         }
108                 }
109                 
110                 [LocalizableAttribute (true)]
111                 [DefaultValueAttribute ("")]
112                 [WebSysDescription ("")]
113                 [WebCategoryAttribute ("Appearance")]
114                 public virtual string Text {
115                         get {
116                                 object ob = ViewState ["Text"];
117                                 if (ob != null) return (string) ob;
118                                 return "";
119                         }
120                         set {
121                                 ViewState ["Text"] = value;
122                                 OnFieldChanged ();
123                         }
124                 }
125                 
126                 public override bool Initialize (bool sortingEnabled, Control control)
127                 {
128                         return base.Initialize (sortingEnabled, control);
129                 }
130                 
131                 protected virtual string FormatDataTextValue (object value)
132                 {
133                         if (DataTextFormatString.Length > 0)
134                                 return string.Format (DataTextFormatString, value);
135                         else if (value == null)
136                                 return string.Empty;
137                         else
138                                 return value.ToString ();
139                 }
140                 
141                 public override void InitializeCell (DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
142                 {
143                         string index = rowIndex.ToString ();
144                         
145                         if (cellType == DataControlCellType.DataCell) {
146                                 DataControlButton btn = new DataControlButton (Control);
147                                 btn.CommandName = CommandName;
148                                 btn.CommandArgument = index;
149                                 
150                                 if (DataTextField != "") {
151                                         if ((rowState & DataControlRowState.Insert) == 0)
152                                                 cell.DataBinding += new EventHandler (OnDataBindField);
153                                 }
154                                 else {
155                                         btn.Text = Text;
156                                         btn.ButtonType = ButtonType;
157                                         if (ButtonType == ButtonType.Image) btn.ImageUrl = ImageUrl;
158                                 }
159                                 cell.Controls.Add (btn);
160                         }
161                         else
162                                 base.InitializeCell (cell, cellType, rowState, rowIndex);
163                 }
164                 
165                 void OnDataBindField (object sender, EventArgs e)
166                 {
167                         DataControlFieldCell cell = (DataControlFieldCell) sender;
168                         DataControlButton btn = (DataControlButton) cell.Controls [0]; 
169                         btn.Text = FormatDataTextValue (GetBoundValue (cell.BindingContainer));
170                         if (ButtonType == ButtonType.Image) btn.ImageUrl = ImageUrl;
171                         btn.ButtonType = ButtonType;
172                 }
173                 
174                 object GetBoundValue (Control controlContainer)
175                 {
176                         IDataItemContainer dic = controlContainer as IDataItemContainer;
177                         if (boundProperty == null) {
178                                 boundProperty = TypeDescriptor.GetProperties (dic.DataItem) [DataTextField];
179                                 if (boundProperty == null)
180                                         new InvalidOperationException ("Property '" + DataTextField + "' not found in object of type " + dic.DataItem.GetType());
181                         }
182                         return boundProperty.GetValue (dic.DataItem);
183                 }
184                 
185                 protected override DataControlField CreateField ()
186                 {
187                         return new ButtonField ();
188                 }
189                 
190                 protected override void CopyProperties (DataControlField newField)
191                 {
192                         base.CopyProperties (newField);
193                         ButtonField field = (ButtonField) newField;
194                         field.CommandName = CommandName;
195                         field.DataTextField = DataTextField;
196                         field.DataTextFormatString = DataTextFormatString;
197                         field.ImageUrl = ImageUrl;
198                         field.Text = Text;
199                 }
200
201                 [MonoTODO]
202                 public override void ValidateSupportsCallback ()
203                 {
204                         throw new NotImplementedException ();
205                 }
206         }
207 }
208 #endif