2
0

ButtonColumn.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // System.Web.UI.WebControls.ButtonColumn.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Gaurav Vaish (2002)
  9. // (C) 2003 Andreas Nahr
  10. //
  11. using System;
  12. using System.ComponentModel;
  13. using System.Web;
  14. using System.Web.UI;
  15. namespace System.Web.UI.WebControls
  16. {
  17. public class ButtonColumn : DataGridColumn
  18. {
  19. private PropertyDescriptor textFieldDescriptor;
  20. public ButtonColumn(): base()
  21. {
  22. }
  23. public override void Initialize()
  24. {
  25. base.Initialize();
  26. textFieldDescriptor = null;
  27. }
  28. public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
  29. {
  30. base.InitializeCell(cell, columnIndex, itemType);
  31. if (Enum.IsDefined(typeof(ListItemType), itemType) &&
  32. itemType != ListItemType.Footer &&
  33. itemType != ListItemType.Header)
  34. {
  35. WebControl toDisplay = null;
  36. if(ButtonType == ButtonColumnType.PushButton)
  37. {
  38. Button b = new Button();
  39. b.Text = Text;
  40. b.CommandName = CommandName;
  41. b.CausesValidation = false;
  42. toDisplay = b;
  43. } else
  44. {
  45. LinkButton lb = new LinkButton();
  46. lb.Text = Text;
  47. lb.CommandName = CommandName;
  48. lb.CausesValidation = false;
  49. toDisplay = lb;
  50. }
  51. if(DataTextField.Length > 0)
  52. {
  53. toDisplay.DataBinding += new EventHandler(OnDataBindButtonColumn);
  54. }
  55. cell.Controls.Add(toDisplay);
  56. }
  57. }
  58. private void OnDataBindButtonColumn(object sender, EventArgs e)
  59. {
  60. Control ctrl = (Control)sender;
  61. object item = ((DataGridItem)ctrl.NamingContainer).DataItem;
  62. if(textFieldDescriptor == null)
  63. {
  64. textFieldDescriptor = TypeDescriptor.GetProperties(item).Find(DataTextField, true);
  65. if(textFieldDescriptor == null && !DesignMode)
  66. throw new HttpException(HttpRuntime.FormatResourceString("Field_Not_Found", DataTextField));
  67. }
  68. string text;
  69. if(textFieldDescriptor != null)
  70. {
  71. text = FormatDataTextValue(textFieldDescriptor.GetValue(item));
  72. } else
  73. {
  74. text = "Sample_DataBound_Text";
  75. }
  76. if(ctrl is LinkButton)
  77. {
  78. ((LinkButton)ctrl).Text = text;
  79. }
  80. else
  81. {
  82. ((Button)ctrl).Text = text;
  83. }
  84. }
  85. protected virtual string FormatDataTextValue(object dataTextValue)
  86. {
  87. string retVal = null;
  88. if(dataTextValue != null)
  89. {
  90. if(DataTextFormatString.Length > 0)
  91. {
  92. retVal = String.Format((string)dataTextValue, DataTextFormatString);
  93. }
  94. else
  95. {
  96. retVal = dataTextValue.ToString();
  97. }
  98. }
  99. return retVal;
  100. }
  101. // LAMESPEC The framework uses Description values for metadata here. However they should be WebSysDescriptions
  102. // because all metadata in this namespace has WebSysDescriptions
  103. [DefaultValue (typeof (ButtonColumnType), "LinkButton"), WebCategory ("Misc")]
  104. [Description ("The type of button used in this column.")]
  105. public virtual ButtonColumnType ButtonType
  106. {
  107. get
  108. {
  109. object o = ViewState["ButtonType"];
  110. if(o!=null)
  111. return (ButtonColumnType)o;
  112. return ButtonColumnType.LinkButton;
  113. }
  114. set
  115. {
  116. if(!System.Enum.IsDefined(typeof(ButtonColumnType), value))
  117. throw new ArgumentException();
  118. ViewState["ButtonType"] = value;
  119. }
  120. }
  121. [DefaultValue (""), WebCategory ("Misc")]
  122. [Description ("The command assigned to this column.")]
  123. public virtual string CommandName
  124. {
  125. get
  126. {
  127. string cn = (string)ViewState["CommandName"];
  128. if(cn!=null)
  129. return cn;
  130. return String.Empty;
  131. }
  132. set
  133. {
  134. ViewState["CommandName"] = value;
  135. }
  136. }
  137. [DefaultValue (""), WebCategory ("Misc")]
  138. [Description ("The datafield that is bound to the text property.")]
  139. public virtual string DataTextField
  140. {
  141. get
  142. {
  143. string dtf = (string)ViewState["DataTextField"];
  144. if(dtf!=null)
  145. return dtf;
  146. return String.Empty;
  147. }
  148. set
  149. {
  150. ViewState["DataTextField"] = value;
  151. }
  152. }
  153. [DefaultValue (""), WebCategory ("Misc")]
  154. [Description ("A format that is applied to the bound text property.")]
  155. public virtual string DataTextFormatString
  156. {
  157. get
  158. {
  159. string dtfs = (string)ViewState["DataTextFormatString"];
  160. if(dtfs!=null)
  161. return dtfs;
  162. return String.Empty;
  163. }
  164. set
  165. {
  166. ViewState["DataTextFormatString"] = value;
  167. }
  168. }
  169. [DefaultValue (""), WebCategory ("Misc")]
  170. [Description ("The text used for this button.")]
  171. public virtual string Text
  172. {
  173. get
  174. {
  175. string text = (string)ViewState["Text"];
  176. if(text!=null)
  177. return text;
  178. return String.Empty;
  179. }
  180. set
  181. {
  182. ViewState["Text"] = value;
  183. }
  184. }
  185. }
  186. }