BaseDataListDesigner.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. /**
  22. * Namespace: System.Web.UI.Design.WebControls
  23. * Class: BaseDataListDesigner
  24. *
  25. * Author: Gaurav Vaish
  26. * Maintainer: mastergaurav AT users DOT sf DOT net
  27. *
  28. * (C) Gaurav Vaish (2002)
  29. */
  30. using System;
  31. using System.Collections;
  32. using System.ComponentModel;
  33. using System.ComponentModel.Design;
  34. using System.Reflection;
  35. using System.Web.UI.Design;
  36. using System.Web.UI;
  37. using System.Web.UI.WebControls;
  38. using System.Data;
  39. namespace System.Web.UI.Design.WebControls
  40. {
  41. public abstract class BaseDataListDesigner : TemplatedControlDesigner,
  42. IDataSourceProvider
  43. {
  44. private BaseDataList baseDataList;
  45. private DataTable desTimeDataTable;
  46. private DataTable dummyDataTable;
  47. private static readonly string[] validNames = new string[] {
  48. "AlternatingItemStyle",
  49. "BackColor",
  50. "DataSource",
  51. "DataMember",
  52. "EditItemStyle",
  53. "Font",
  54. "ForeColor",
  55. "HeaderStyle",
  56. "FooterStyle",
  57. "ItemStyle",
  58. "SelectedItemStyle",
  59. "SeparatorStyle"
  60. };
  61. public BaseDataListDesigner()
  62. {
  63. }
  64. public string DataKeyField
  65. {
  66. get
  67. {
  68. return baseDataList.DataKeyField;
  69. }
  70. set
  71. {
  72. baseDataList.DataKeyField = value;
  73. }
  74. }
  75. public string DataMember
  76. {
  77. get
  78. {
  79. return baseDataList.DataMember;
  80. }
  81. set
  82. {
  83. baseDataList.DataMember = value;
  84. }
  85. }
  86. public string DataSource
  87. {
  88. get
  89. {
  90. DataBinding element = DataBindings["DataSource"];
  91. if(element != null)
  92. {
  93. return element.Expression;
  94. }
  95. return String.Empty;
  96. }
  97. set
  98. {
  99. if(value == null && value.Length == 0)
  100. {
  101. DataBindings.Remove("DataSource");
  102. } else
  103. {
  104. DataBinding element = DataBindings["DataSource"];
  105. if(element == null)
  106. {
  107. element = new DataBinding("DataSource",
  108. typeof(IEnumerable),
  109. value);
  110. } else
  111. {
  112. element.Expression = value;
  113. }
  114. DataBindings.Add(element);
  115. }
  116. OnDataSourceChanged();
  117. OnBindingsCollectionChanged("DataSource");
  118. }
  119. }
  120. protected internal virtual void OnDataSourceChanged()
  121. {
  122. desTimeDataTable = null;
  123. }
  124. public override bool DesignTimeHtmlRequiresLoadComplete
  125. {
  126. get
  127. {
  128. return (DataSource.Length > 0);
  129. }
  130. }
  131. public override DesignerVerbCollection Verbs
  132. {
  133. get
  134. {
  135. throw new NotImplementedException();
  136. }
  137. }
  138. public object GetSelectedDataSource()
  139. {
  140. object retVal = null;
  141. DataBinding element = DataBindings["DataSource"];
  142. if(element != null)
  143. {
  144. retVal = DesignTimeData.GetSelectedDataSource(baseDataList,
  145. element.Expression);
  146. }
  147. return retVal;
  148. }
  149. public IEnumerable GetResolvedSelectedDataSource()
  150. {
  151. IEnumerable retVal = null;
  152. DataBinding element = DataBindings["DataSource"];
  153. if(element != null)
  154. {
  155. retVal = DesignTimeData.GetSelectedDataSource(baseDataList,
  156. element.Expression,
  157. DataMember);
  158. }
  159. return retVal;
  160. }
  161. public override IEnumerable GetTemplateContainerDataSource(
  162. string templateName)
  163. {
  164. return GetResolvedSelectedDataSource();
  165. }
  166. public override void Initialize(IComponent component)
  167. {
  168. baseDataList = (BaseDataList)component;
  169. base.Initialize(component);
  170. }
  171. public override void OnComponentChanged(object sender,
  172. ComponentChangedEventArgs e)
  173. {
  174. if(e.Member != null)
  175. {
  176. string name = e.Member.Name;
  177. foreach(string current in validNames)
  178. {
  179. if(name == current)
  180. {
  181. OnStylesChanged();
  182. break;
  183. }
  184. }
  185. }
  186. base.OnComponentChanged(sender, e);
  187. }
  188. protected internal void OnStylesChanged()
  189. {
  190. OnTemplateEditingVerbsChanged();
  191. }
  192. protected abstract void OnTemplateEditingVerbsChanged();
  193. protected override void Dispose(bool disposing)
  194. {
  195. if(disposing)
  196. baseDataList = null;
  197. base.Dispose(disposing);
  198. }
  199. protected IEnumerable GetDesignTimeDataSource(int minimumRows,
  200. out bool dummyDataSource)
  201. {
  202. return GetDesignTimeDataSource(GetResolvedSelectedDataSource(),
  203. minimumRows,
  204. out dummyDataSource);
  205. }
  206. protected IEnumerable GetDesignTimeDataSource(IEnumerable selectedDataSource,
  207. int minimumRows,
  208. out bool dummyDataSource)
  209. {
  210. DataTable toDeploy = desTimeDataTable;
  211. dummyDataSource = false;
  212. if(minimumRows == 0)
  213. {
  214. if(selectedDataSource != null)
  215. {
  216. desTimeDataTable = DesignTimeData.CreateSampleDataTable(
  217. selectedDataSource);
  218. toDeploy = desTimeDataTable;
  219. }
  220. if(toDeploy == null)
  221. {
  222. if(dummyDataTable == null)
  223. dummyDataTable = DesignTimeData.CreateDummyDataTable();
  224. toDeploy = dummyDataTable;
  225. dummyDataSource = true;
  226. }
  227. }
  228. return DesignTimeData.GetDesignTimeDataSource(toDeploy, minimumRows);
  229. }
  230. }
  231. }