2 // System.Web.UI.WebControls.ButtonColumn.cs
5 // Dick Porter <dick@ximian.com>
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.ComponentModel;
31 using System.Security.Permissions;
33 namespace System.Web.UI.WebControls {
36 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38 public class ButtonColumn : DataGridColumn {
40 [DefaultValue(ButtonColumnType.LinkButton)]
42 [WebSysDescription("The type of button contained within the column.")]
44 [Description("The type of button contained within the column.")]
46 [WebCategory ("Misc")]
47 public virtual ButtonColumnType ButtonType
50 return (ButtonColumnType) ViewState.GetInt ("LinkButton",
51 (int) ButtonColumnType.LinkButton);
54 ViewState ["LinkButton"] = value;
60 [WebSysDescription("The command associated with the button.")]
62 [Description("The command associated with the button.")]
64 [WebCategory ("Misc")]
65 public virtual string CommandName
68 return ViewState.GetString ("CommandName", String.Empty);
71 ViewState ["CommandName"] = value;
76 [DefaultValue (false)]
77 [WebSysDescription("")]
78 [WebCategory ("Behavior")]
79 public virtual bool CausesValidation
82 return ViewState.GetBool ("CausesValidation", false);
85 ViewState ["CausesValidation"] = value;
92 [WebSysDescription("The field bound to the text property of the button.")]
94 [Description("The field bound to the text property of the button.")]
96 [WebCategory ("Misc")]
97 public virtual string DataTextField
100 return ViewState.GetString ("DataTextField", String.Empty);
103 ViewState ["DataTextField"] = value;
109 [WebSysDescription("The formatting applied to the value bound to the Text property.")]
111 [Description("The formatting applied to the value bound to the Text property.")]
113 [WebCategory ("Misc")]
114 public virtual string DataTextFormatString
117 return ViewState.GetString ("DataTextFormatString",
121 ViewState ["DataTextFormatString"] = value;
129 [WebSysDescription("The text used for the button.")]
131 [Description("The text used for the button.")]
133 [WebCategory ("Misc")]
134 public virtual string Text
137 return ViewState.GetString ("Text", String.Empty);
140 ViewState ["Text"] = value;
146 [WebSysDescription("")]
147 [WebCategory ("Behavior")]
148 public virtual string ValidationGroup
151 return ViewState.GetString ("ValidationGroup", "");
154 ViewState ["ValidationGroup"] = value;
159 public override void Initialize ()
161 /* No documentation for this method, so it's
162 * only here to keep corcompare quiet
167 public override void InitializeCell (TableCell cell,
169 ListItemType itemType)
171 base.InitializeCell (cell, columnIndex, itemType);
173 if (itemType != ListItemType.Header &&
174 itemType != ListItemType.Footer) {
175 switch (ButtonType) {
176 case ButtonColumnType.LinkButton:
178 LinkButton butt = new ForeColorLinkButton ();
181 butt.CommandName = CommandName;
183 if (DataTextField != "")
184 butt.DataBinding += new EventHandler (DoDataBind);
186 cell.Controls.Add (butt);
190 case ButtonColumnType.PushButton:
192 Button butt = new Button ();
195 butt.CommandName = CommandName;
197 if (DataTextField != "")
198 butt.DataBinding += new EventHandler (DoDataBind);
199 cell.Controls.Add (butt);
208 string GetValueFromItem (DataGridItem item)
211 if (text_field == null)
212 text_field = DataTextField;
214 if (text_field != "")
215 val = DataBinder.Eval (item.DataItem, text_field);
217 return FormatDataTextValue (val);
220 void DoDataBind (object sender, EventArgs e)
222 Control ctrl = (Control) sender;
223 string text = GetValueFromItem ((DataGridItem) ctrl.NamingContainer);
225 LinkButton lb = sender as LinkButton;
227 Button b = (Button) sender;
235 protected virtual string FormatDataTextValue (object dataTextValue)
237 if (dataTextValue == null)
241 format = DataTextFormatString;
244 return dataTextValue.ToString ();
246 return String.Format (format, dataTextValue);