BaseDataList.cs 4.5 KB

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