ButtonField.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // System.Web.UI.WebControls.ButtonField.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. //
  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:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  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.
  28. //
  29. #if NET_2_0
  30. using System.Reflection;
  31. using System.Collections;
  32. using System.Collections.Specialized;
  33. using System.Web.UI;
  34. using System.ComponentModel;
  35. using System.Security.Permissions;
  36. namespace System.Web.UI.WebControls {
  37. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  38. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  39. public class ButtonField : ButtonFieldBase
  40. {
  41. PropertyDescriptor boundProperty;
  42. [DefaultValueAttribute ("")]
  43. [WebSysDescription ("Raised when a Button Command is executed.")]
  44. [WebCategoryAttribute ("Behavior")]
  45. public virtual string CommandName {
  46. get {
  47. object ob = ViewState ["CommandName"];
  48. if (ob != null) return (string) ob;
  49. return "";
  50. }
  51. set {
  52. ViewState ["CommandName"] = value;
  53. OnFieldChanged ();
  54. }
  55. }
  56. [DefaultValueAttribute ("")]
  57. [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
  58. [WebSysDescription ("")]
  59. [WebCategoryAttribute ("Data")]
  60. public virtual string DataTextField {
  61. get {
  62. object ob = ViewState ["DataTextField"];
  63. if (ob != null) return (string) ob;
  64. return "";
  65. }
  66. set {
  67. ViewState ["DataTextField"] = value;
  68. OnFieldChanged ();
  69. }
  70. }
  71. [DefaultValueAttribute ("")]
  72. [WebSysDescription ("")]
  73. [WebCategoryAttribute ("Data")]
  74. public virtual string DataTextFormatString {
  75. get {
  76. object ob = ViewState ["DataTextFormatString"];
  77. if (ob != null) return (string) ob;
  78. return "";
  79. }
  80. set {
  81. ViewState ["DataTextFormatString"] = value;
  82. OnFieldChanged ();
  83. }
  84. }
  85. [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  86. [WebSysDescription ("")]
  87. [WebCategoryAttribute ("Appearance")]
  88. [DefaultValueAttribute ("")]
  89. [UrlPropertyAttribute]
  90. public virtual string ImageUrl {
  91. get {
  92. object ob = ViewState ["ImageUrl"];
  93. if (ob != null) return (string) ob;
  94. return "";
  95. }
  96. set {
  97. ViewState ["ImageUrl"] = value;
  98. OnFieldChanged ();
  99. }
  100. }
  101. [LocalizableAttribute (true)]
  102. [DefaultValueAttribute ("")]
  103. [WebSysDescription ("")]
  104. [WebCategoryAttribute ("Appearance")]
  105. public virtual string Text {
  106. get {
  107. object ob = ViewState ["Text"];
  108. if (ob != null) return (string) ob;
  109. return "";
  110. }
  111. set {
  112. ViewState ["Text"] = value;
  113. OnFieldChanged ();
  114. }
  115. }
  116. public override bool Initialize (bool sortingEnabled, Control control)
  117. {
  118. return base.Initialize (sortingEnabled, control);
  119. }
  120. protected virtual string FormatDataTextValue (object value)
  121. {
  122. if (DataTextFormatString.Length > 0)
  123. return string.Format (DataTextFormatString, value);
  124. else if (value == null)
  125. return string.Empty;
  126. else
  127. return value.ToString ();
  128. }
  129. public override void InitializeCell (DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
  130. {
  131. string index = rowIndex.ToString ();
  132. if (cellType == DataControlCellType.DataCell) {
  133. DataControlButton btn = new DataControlButton (Control);
  134. btn.CommandName = CommandName;
  135. btn.CommandArgument = index;
  136. if (DataTextField != "") {
  137. if ((rowState & DataControlRowState.Insert) == 0)
  138. cell.DataBinding += new EventHandler (OnDataBindField);
  139. }
  140. else {
  141. btn.Text = Text;
  142. btn.ButtonType = ButtonType;
  143. if (ButtonType == ButtonType.Image) btn.ImageUrl = ImageUrl;
  144. }
  145. cell.Controls.Add (btn);
  146. }
  147. else
  148. base.InitializeCell (cell, cellType, rowState, rowIndex);
  149. }
  150. void OnDataBindField (object sender, EventArgs e)
  151. {
  152. DataControlFieldCell cell = (DataControlFieldCell) sender;
  153. DataControlButton btn = (DataControlButton) cell.Controls [0];
  154. btn.Text = FormatDataTextValue (GetBoundValue (cell.BindingContainer));
  155. if (ButtonType == ButtonType.Image) btn.ImageUrl = ImageUrl;
  156. btn.ButtonType = ButtonType;
  157. }
  158. object GetBoundValue (Control controlContainer)
  159. {
  160. IDataItemContainer dic = controlContainer as IDataItemContainer;
  161. if (boundProperty == null) {
  162. boundProperty = TypeDescriptor.GetProperties (dic.DataItem) [DataTextField];
  163. if (boundProperty == null)
  164. new InvalidOperationException ("Property '" + DataTextField + "' not found in object of type " + dic.DataItem.GetType());
  165. }
  166. return boundProperty.GetValue (dic.DataItem);
  167. }
  168. protected override DataControlField CreateField ()
  169. {
  170. return new ButtonField ();
  171. }
  172. protected override void CopyProperties (DataControlField newField)
  173. {
  174. base.CopyProperties (newField);
  175. ButtonField field = (ButtonField) newField;
  176. field.CommandName = CommandName;
  177. field.DataTextField = DataTextField;
  178. field.DataTextFormatString = DataTextFormatString;
  179. field.ImageUrl = ImageUrl;
  180. field.Text = Text;
  181. }
  182. }
  183. }
  184. #endif