2 // System.Web.UI.WebControls.ButtonField.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
7 // (C) 2005 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System.Reflection;
33 using System.Collections;
34 using System.Collections.Specialized;
36 using System.ComponentModel;
37 using System.Security.Permissions;
39 namespace System.Web.UI.WebControls {
41 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43 public class ButtonField : ButtonFieldBase
45 PropertyDescriptor boundProperty;
47 [DefaultValueAttribute ("")]
48 [WebSysDescription ("Raised when a Button Command is executed.")]
49 [WebCategoryAttribute ("Behavior")]
50 public virtual string CommandName {
52 object ob = ViewState ["CommandName"];
53 if (ob != null) return (string) ob;
57 ViewState ["CommandName"] = value;
62 [DefaultValueAttribute ("")]
63 [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
64 [WebSysDescription ("")]
65 [WebCategoryAttribute ("Data")]
66 public virtual string DataTextField {
68 object ob = ViewState ["DataTextField"];
69 if (ob != null) return (string) ob;
73 ViewState ["DataTextField"] = value;
78 [DefaultValueAttribute ("")]
\r
79 [WebSysDescription ("")]
80 [WebCategoryAttribute ("Data")]
81 public virtual string DataTextFormatString {
83 object ob = ViewState ["DataTextFormatString"];
84 if (ob != null) return (string) ob;
88 ViewState ["DataTextFormatString"] = value;
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 {
100 object ob = ViewState ["ImageUrl"];
101 if (ob != null) return (string) ob;
105 ViewState ["ImageUrl"] = value;
110 [LocalizableAttribute (true)]
111 [DefaultValueAttribute ("")]
112 [WebSysDescription ("")]
113 [WebCategoryAttribute ("Appearance")]
114 public virtual string Text {
116 object ob = ViewState ["Text"];
117 if (ob != null) return (string) ob;
121 ViewState ["Text"] = value;
126 public override bool Initialize (bool sortingEnabled, Control control)
128 return base.Initialize (sortingEnabled, control);
131 protected virtual string FormatDataTextValue (object value)
133 if (DataTextFormatString.Length > 0)
134 return string.Format (DataTextFormatString, value);
135 else if (value == null)
138 return value.ToString ();
141 public override void InitializeCell (DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
143 string index = rowIndex.ToString ();
145 if (cellType == DataControlCellType.DataCell) {
146 DataControlButton btn = new DataControlButton (Control);
147 btn.CommandName = CommandName;
148 btn.CommandArgument = index;
150 if (DataTextField != "") {
151 if ((rowState & DataControlRowState.Insert) == 0)
152 cell.DataBinding += new EventHandler (OnDataBindField);
156 btn.ButtonType = ButtonType;
157 if (ButtonType == ButtonType.Image) btn.ImageUrl = ImageUrl;
159 cell.Controls.Add (btn);
162 base.InitializeCell (cell, cellType, rowState, rowIndex);
165 void OnDataBindField (object sender, EventArgs e)
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;
174 object GetBoundValue (Control controlContainer)
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());
182 return boundProperty.GetValue (dic.DataItem);
185 protected override DataControlField CreateField ()
187 return new ButtonField ();
190 protected override void CopyProperties (DataControlField newField)
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;
202 public override void ValidateSupportsCallback ()
204 throw new NotImplementedException ();