ButtonField.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. [WebCategoryAttribute ("Behavior")]
  43. [DefaultValueAttribute ("")]
  44. public virtual string CommandName {
  45. get {
  46. object ob = ViewState ["CommandName"];
  47. if (ob != null) return (string) ob;
  48. return "";
  49. }
  50. set {
  51. ViewState ["CommandName"] = value;
  52. OnFieldChanged ();
  53. }
  54. }
  55. [WebCategoryAttribute ("Data")]
  56. [DefaultValueAttribute ("")]
  57. public virtual string DataTextField {
  58. get {
  59. object ob = ViewState ["DataTextField"];
  60. if (ob != null) return (string) ob;
  61. return "";
  62. }
  63. set {
  64. ViewState ["DataTextField"] = value;
  65. OnFieldChanged ();
  66. }
  67. }
  68. [WebCategoryAttribute ("Data")]
  69. [DefaultValueAttribute ("")]
  70. public virtual string DataTextFormatString {
  71. get {
  72. object ob = ViewState ["DataTextFormatString"];
  73. if (ob != null) return (string) ob;
  74. return "";
  75. }
  76. set {
  77. ViewState ["DataTextFormatString"] = value;
  78. OnFieldChanged ();
  79. }
  80. }
  81. [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
  82. [WebCategoryAttribute ("Appearance")]
  83. [DefaultValueAttribute ("")]
  84. [UrlPropertyAttribute]
  85. public virtual string ImageUrl {
  86. get {
  87. object ob = ViewState ["ImageUrl"];
  88. if (ob != null) return (string) ob;
  89. return "";
  90. }
  91. set {
  92. ViewState ["ImageUrl"] = value;
  93. OnFieldChanged ();
  94. }
  95. }
  96. [LocalizableAttribute (true)]
  97. [WebCategoryAttribute ("Appearance")]
  98. [DefaultValueAttribute ("")]
  99. public virtual string Text {
  100. get {
  101. object ob = ViewState ["Text"];
  102. if (ob != null) return (string) ob;
  103. return "";
  104. }
  105. set {
  106. ViewState ["Text"] = value;
  107. OnFieldChanged ();
  108. }
  109. }
  110. public override bool Initialize (bool sortingEnabled, Control control)
  111. {
  112. return base.Initialize (sortingEnabled, control);
  113. }
  114. protected virtual string FormatDataTextValue (object value)
  115. {
  116. if (DataTextFormatString.Length > 0)
  117. return string.Format (DataTextFormatString, value);
  118. else if (value == null)
  119. return string.Empty;
  120. else
  121. return value.ToString ();
  122. }
  123. public override void InitializeCell (DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
  124. {
  125. string index = rowIndex.ToString ();
  126. if (cellType == DataControlCellType.DataCell) {
  127. DataControlButton btn = new DataControlButton (Control);
  128. btn.CommandName = CommandName;
  129. btn.CommandArgument = index;
  130. if (DataTextField != "") {
  131. cell.DataBinding += new EventHandler (OnDataBindField);
  132. }
  133. else {
  134. btn.Text = Text;
  135. btn.ButtonType = ButtonType;
  136. if (ButtonType == ButtonType.Image) btn.ImageUrl = ImageUrl;
  137. }
  138. cell.Controls.Add (btn);
  139. }
  140. else
  141. base.InitializeCell (cell, cellType, rowState, rowIndex);
  142. }
  143. protected virtual void OnDataBindField (object sender, EventArgs e)
  144. {
  145. DataControlFieldCell cell = (DataControlFieldCell) sender;
  146. DataControlButton btn = (DataControlButton) cell.Controls [0];
  147. btn.Text = FormatDataTextValue (GetBoundValue (cell.BindingContainer));
  148. if (ButtonType == ButtonType.Image) btn.ImageUrl = ImageUrl;
  149. btn.ButtonType = ButtonType;
  150. }
  151. object GetBoundValue (Control controlContainer)
  152. {
  153. IDataItemContainer dic = controlContainer as IDataItemContainer;
  154. if (boundProperty == null) {
  155. ICustomTypeDescriptor desc = dic.DataItem as ICustomTypeDescriptor;
  156. if (desc != null) {
  157. boundProperty = desc.GetProperties () [DataTextField];
  158. if (boundProperty != null)
  159. return boundProperty.GetValue (dic.DataItem);
  160. } else {
  161. PropertyInfo pi = dic.DataItem.GetType ().GetProperty (DataTextField);
  162. if (pi != null)
  163. return pi.GetValue (dic.DataItem, null);
  164. }
  165. throw new InvalidOperationException ("Property '" + DataTextField + "' not found in data bound item");
  166. }
  167. return boundProperty.GetValue (dic.DataItem);
  168. }
  169. }
  170. }
  171. #endif