BaseDataList.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: BaseDataList
  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 (2001)
  12. */
  13. using System;
  14. using System.Collections;
  15. using System.Web;
  16. using System.Web.UI;
  17. namespace System.Web.UI.WebControls
  18. {
  19. public abstract class BaseDataList: WebControl
  20. {
  21. private static readonly object SelectedIndexChangedEvent = new object();
  22. internal static string ItemCountViewStateKey = "_!ItemCount";
  23. private DataKeyCollection dataKeys;
  24. private object dataSource;
  25. public BaseDataList() : base()
  26. {
  27. }
  28. public static bool IsBindableType(Type type)
  29. {
  30. if(type.IsPrimitive || type == typeof(string) || type == typeof(DateTime) || type == typeof(Decimal))
  31. return true;
  32. return false;
  33. }
  34. public override void DataBind()
  35. {
  36. OnDataBinding(EventArgs.Empty);
  37. }
  38. public event EventHandler SelectedIndexChanged
  39. {
  40. add
  41. {
  42. Events.AddHandler(SelectedIndexChangedEvent, value);
  43. }
  44. remove
  45. {
  46. Events.RemoveHandler(SelectedIndexChangedEvent, value);
  47. }
  48. }
  49. public virtual int CellPadding
  50. {
  51. get
  52. {
  53. if(!ControlStyleCreated)
  54. retrurn -1;
  55. return ((TableStyle)ControlStyle).CellPadding;
  56. }
  57. set
  58. {
  59. ((TableStyle)ControlStyle).CellPadding = value;
  60. }
  61. }
  62. public virtual int CellSpacing
  63. {
  64. get
  65. {
  66. if(!ControlStyleCreated)
  67. retrurn -1;
  68. return ((TableStyle)ControlStyle).CellSpacing;
  69. }
  70. set
  71. {
  72. ((TableStyle)ControlStyle).CellSpacing = value;
  73. }
  74. }
  75. public virtual string DataKeyField
  76. {
  77. get
  78. {
  79. object o = ViewState["DataKeyField"];
  80. if(o!=null)
  81. return (string)o;
  82. return String.Empty;
  83. }
  84. set
  85. {
  86. ViewState["DataKeyField"] = value;
  87. }
  88. }
  89. public DataKeyCollection DataKeys
  90. {
  91. get
  92. {
  93. if( !(dataKeys) )
  94. dataKeys = new DataKeyCollection(DataKeysArray);
  95. return dataKeys;
  96. }
  97. }
  98. public string DataMember
  99. {
  100. get
  101. {
  102. object o = ViewState["DataMember"];
  103. if(o!=null)
  104. return (string)o;
  105. return String.Empty;
  106. }
  107. set
  108. {
  109. ViewState["DataMember"] = value;
  110. }
  111. }
  112. public virtual object DataSource
  113. {
  114. get
  115. {
  116. return dataSource;
  117. }
  118. set
  119. {
  120. if( (value) && ( value is IListSource || value is IEnumerable) )
  121. {
  122. dataSource = value;
  123. } else
  124. {
  125. throw new ArgumentException(HttpRuntime.FormatResourceString("Invalid_DataSource_Type", ID));
  126. }
  127. }
  128. }
  129. public virtual GridLines GridLines
  130. {
  131. get
  132. {
  133. if(ControlStyleCreated)
  134. return ((TableStyle)ControlStyle).GridLines;
  135. return GridLines.Both;
  136. }
  137. set
  138. {
  139. ((TableStyle)ControlStyle).GridLines = value;
  140. }
  141. }
  142. public virtual HorizontalAlign HorizontalAlign
  143. {
  144. get
  145. {
  146. if(ControlStyleCreated)
  147. return ((TableStyle)ControlStyle).HorizontalAlign;
  148. return HorizontalAlign.NotSet;
  149. }
  150. set
  151. {
  152. ((TableStyle)ControlStyle).HorizontalAlign = value;
  153. }
  154. }
  155. protected ArrayList DataKeysArray
  156. {
  157. get
  158. {
  159. object o = ViewState["DataKeys"];
  160. if(o == null)
  161. {
  162. o = new ArrayList();
  163. ViewState["DataKeys"] = o;
  164. }
  165. return (ArrayList)o;
  166. }
  167. }
  168. protected override void AddParsedSubObject(object o)
  169. {
  170. // Preventing literal controls from being added as children.
  171. }
  172. protected override void CreateChildControls()
  173. {
  174. Controls.Clear();
  175. if(ViewState[ItemCountViewStateKey]!=null)
  176. {
  177. CreateControlHierarchy(true);
  178. ClearChildViewState();
  179. }
  180. }
  181. protected override void OnDataBinding(EventArgs e)
  182. {
  183. base.OnDataBinding(e);
  184. Controls.Clear();
  185. ClearChildViewState();
  186. CreateControlHierarchy(true);
  187. ChildControlsCreated = true;
  188. TrackViewState();
  189. }
  190. protected virtual void OnSelectedIndexChanged(EventArgs e)
  191. {
  192. if(Events != null)
  193. {
  194. EventHandler eh = (EventHandler)(Events[SelectedIndexChangedEvent]);
  195. if(eh!=null)
  196. eh(this, e);
  197. }
  198. }
  199. protected override void Render(HtmlTextWriter writer)
  200. {
  201. PrepareControlHierarchy();
  202. base.RenderContents(writer);
  203. }
  204. protected abstract void PrepareControlHierarchy();
  205. protected abstract void CreateControlHierarchy(bool useDataSource);
  206. }
  207. }