ButtonColumn.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: ButtonColumn
  4. *
  5. * Author: Gaurav Vaish
  6. * Maintainer: [email protected]
  7. * Contact: <[email protected]>, <[email protected]>
  8. * Implementation: yes
  9. * Status: 100%
  10. *
  11. * (C) Gaurav Vaish (2002)
  12. */
  13. using System;
  14. using System.ComponentModel;
  15. using System.Web;
  16. using System.Web.UI;
  17. namespace System.Web.UI.WebControls
  18. {
  19. public class ButtonColumn : DataGridColumn
  20. {
  21. private PropertyDescriptor textFieldDescriptor;
  22. public ButtonColumn(): base()
  23. {
  24. }
  25. public override void Initialize()
  26. {
  27. Initialize();
  28. textFieldDescriptor = null;
  29. }
  30. public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
  31. {
  32. InitializeCell(cell, columnIndex, itemType);
  33. if(Enum.IsDefined(typeof(ListItemType), itemType) && itemType != ListItemType.Footer)
  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. public virtual ButtonColumnType ButtonType
  102. {
  103. get
  104. {
  105. object o = ViewState["ButtonType"];
  106. if(o!=null)
  107. return (ButtonColumnType)o;
  108. return ButtonColumnType.LinkButton;
  109. }
  110. set
  111. {
  112. if(!System.Enum.IsDefined(typeof(ButtonColumnType), value))
  113. throw new ArgumentException();
  114. ViewState["ButtonType"] = value;
  115. }
  116. }
  117. public virtual string CommandName
  118. {
  119. get
  120. {
  121. string cn = (string)ViewState["CommandName"];
  122. if(cn!=null)
  123. return cn;
  124. return String.Empty;
  125. }
  126. set
  127. {
  128. ViewState["CommandName"] = value;
  129. }
  130. }
  131. public virtual string DataTextField
  132. {
  133. get
  134. {
  135. string dtf = (string)ViewState["DataTextField"];
  136. if(dtf!=null)
  137. return dtf;
  138. return String.Empty;
  139. }
  140. set
  141. {
  142. ViewState["DataTextField"] = value;
  143. }
  144. }
  145. public virtual string DataTextFormatString
  146. {
  147. get
  148. {
  149. string dtfs = (string)ViewState["DataTextFormatString"];
  150. if(dtfs!=null)
  151. return dtfs;
  152. return String.Empty;
  153. }
  154. set
  155. {
  156. ViewState["DataTextFormatString"] = value;
  157. }
  158. }
  159. public virtual string Text
  160. {
  161. get
  162. {
  163. string text = (string)ViewState["Text"];
  164. if(text!=null)
  165. return text;
  166. return String.Empty;
  167. }
  168. set
  169. {
  170. ViewState["Text"] = value;
  171. }
  172. }
  173. }
  174. }