2
0

BoundColumn.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // System.Web.UI.WebControls.BoundColumn.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. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.ComponentModel;
  33. using System.Web;
  34. using System.Web.UI;
  35. namespace System.Web.UI.WebControls
  36. {
  37. public class BoundColumn : DataGridColumn
  38. {
  39. public static readonly string thisExpr = "!";
  40. private bool boundFieldDescriptionValid;
  41. private string boundField;
  42. private string formatting;
  43. private PropertyDescriptor desc;
  44. public BoundColumn(): base()
  45. {
  46. }
  47. public override void Initialize()
  48. {
  49. base.Initialize();
  50. desc = null;
  51. boundField = DataField;
  52. formatting = DataFormatString;
  53. boundFieldDescriptionValid = false;
  54. }
  55. public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
  56. {
  57. base.InitializeCell(cell, columnIndex, itemType);
  58. Control bindCtrl = null;
  59. Control toAdd = null;
  60. switch(itemType)
  61. {
  62. case ListItemType.Item : goto case ListItemType.SelectedItem;
  63. case ListItemType.AlternatingItem
  64. : goto case ListItemType.SelectedItem;
  65. case ListItemType.SelectedItem
  66. : if(DataField.Length != 0)
  67. bindCtrl = cell;
  68. break;
  69. case ListItemType.EditItem
  70. : if(!ReadOnly)
  71. {
  72. TextBox box = new TextBox();
  73. toAdd = box;
  74. if(DataField.Length != 0)
  75. bindCtrl = box;
  76. } else
  77. goto case ListItemType.SelectedItem;
  78. break;
  79. }
  80. if(toAdd != null)
  81. cell.Controls.Add(toAdd);
  82. if(bindCtrl != null)
  83. bindCtrl.DataBinding += new EventHandler(OnDataBindColumn);
  84. //throw new NotImplementedException();
  85. }
  86. private void OnDataBindColumn(object sender, EventArgs e)
  87. {
  88. Control senderCtrl = (Control)sender;
  89. DataGridItem item = (DataGridItem)senderCtrl.NamingContainer;
  90. object data = item.DataItem;
  91. if(!boundFieldDescriptionValid)
  92. {
  93. if(boundField != BoundColumn.thisExpr)
  94. {
  95. desc = TypeDescriptor.GetProperties(data).Find(boundField, true);
  96. if(desc == null && !DesignMode)
  97. {
  98. throw new HttpException(
  99. HttpRuntime.FormatResourceString("File_Not_Found",
  100. boundField));
  101. }
  102. boundFieldDescriptionValid = true;
  103. }
  104. }
  105. object value = data;
  106. string val = String.Empty;
  107. if(desc == null && DesignMode)
  108. {
  109. throw new NotImplementedException();
  110. } else
  111. {
  112. if(desc != null)
  113. value = desc.GetValue(data);
  114. val = FormatDataValue(value);
  115. }
  116. if(senderCtrl is TableCell)
  117. {
  118. if(val.Length == 0)
  119. val = " ";
  120. ((TableCell)senderCtrl).Text = val;
  121. } else
  122. {
  123. ((TextBox)senderCtrl).Text = val;
  124. }
  125. }
  126. [DefaultValue (""), WebCategory ("Misc")]
  127. [WebSysDescription ("The field that this column is bound to.")]
  128. public virtual string DataField
  129. {
  130. get
  131. {
  132. object o = ViewState["DataField"];
  133. if(o != null)
  134. return (string)o;
  135. return String.Empty;
  136. }
  137. set
  138. {
  139. ViewState["DataField"] = value;
  140. OnColumnChanged();
  141. }
  142. }
  143. [DefaultValue (""), WebCategory ("Misc")]
  144. [WebSysDescription ("A format string that is applied to the data value.")]
  145. public virtual string DataFormatString
  146. {
  147. get
  148. {
  149. object o = ViewState["DataFormatString"];
  150. if(o != null)
  151. return (string)o;
  152. return String.Empty;
  153. }
  154. set
  155. {
  156. ViewState["DataFormatString"] = value;
  157. OnColumnChanged();
  158. }
  159. }
  160. [DefaultValue (false), WebCategory ("Misc")]
  161. [WebSysDescription ("Determines if the databound field can only be displayed or also edited.")]
  162. public virtual bool ReadOnly
  163. {
  164. get
  165. {
  166. object o = ViewState["ReadOnly"];
  167. if(o != null)
  168. return (bool)o;
  169. return false;
  170. }
  171. set
  172. {
  173. ViewState["ReadOnly"] = value;
  174. }
  175. }
  176. protected virtual string FormatDataValue(Object dataValue)
  177. {
  178. string retVal = String.Empty;
  179. if(dataValue != null)
  180. {
  181. if(formatting.Length == 0)
  182. retVal = dataValue.ToString();
  183. else
  184. retVal = String.Format(formatting, dataValue);
  185. }
  186. return retVal;
  187. }
  188. }
  189. }