2
0

BaseDataList.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. //
  2. // System.Web.UI.WebControls.BaseDataList.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Gaurav Vaish (2001)
  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.ComponentModel.Design;
  34. using System.Collections;
  35. using System.Web;
  36. using System.Web.UI;
  37. using System.Web.Util;
  38. namespace System.Web.UI.WebControls
  39. {
  40. [DefaultEvent("SelectedIndexChanged")]
  41. [DefaultProperty("DataSource")]
  42. [Designer("System.Web.UI.Design.WebControls.BaseDataListDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
  43. public abstract class BaseDataList: WebControl
  44. {
  45. private static readonly object SelectedIndexChangedEvent = new object();
  46. internal static string ItemCountViewStateKey = "_!ItemCount";
  47. private DataKeyCollection dataKeys;
  48. private object dataSource;
  49. public BaseDataList() : base()
  50. {
  51. }
  52. public static bool IsBindableType(Type type)
  53. {
  54. if(type.IsPrimitive || type == typeof(string) || type == typeof(DateTime) || type == typeof(Decimal))
  55. return true;
  56. return false;
  57. }
  58. public override ControlCollection Controls
  59. {
  60. get
  61. {
  62. EnsureChildControls();
  63. return base.Controls;
  64. }
  65. }
  66. public override void DataBind()
  67. {
  68. #if NET_2_0
  69. RequiresDataBinding = false;
  70. #endif
  71. OnDataBinding(EventArgs.Empty);
  72. }
  73. [WebCategory("Action")]
  74. [WebSysDescription("BaseDataList_OnSelectedIndexChanged")]
  75. public event EventHandler SelectedIndexChanged
  76. {
  77. add
  78. {
  79. Events.AddHandler(SelectedIndexChangedEvent, value);
  80. }
  81. remove
  82. {
  83. Events.RemoveHandler(SelectedIndexChangedEvent, value);
  84. }
  85. }
  86. [Bindable(true)]
  87. [DefaultValue(-1)]
  88. [WebCategory("Layout")]
  89. [WebSysDescription("BaseDataList_CellPadding")]
  90. public virtual int CellPadding
  91. {
  92. get
  93. {
  94. if(!ControlStyleCreated)
  95. return -1;
  96. return ((TableStyle)ControlStyle).CellPadding;
  97. }
  98. set
  99. {
  100. ((TableStyle)ControlStyle).CellPadding = value;
  101. }
  102. }
  103. [Bindable(true)]
  104. [DefaultValue(-1)]
  105. [WebCategory("Layout")]
  106. [WebSysDescription("BaseDataList_CellSpacing")]
  107. public virtual int CellSpacing
  108. {
  109. get
  110. {
  111. if(!ControlStyleCreated)
  112. return -1;
  113. return ((TableStyle)ControlStyle).CellSpacing;
  114. }
  115. set
  116. {
  117. ((TableStyle)ControlStyle).CellSpacing = value;
  118. }
  119. }
  120. [DefaultValue("")]
  121. [WebCategory("Data")]
  122. [WebSysDescription("BaseDataList_DataKeyField")]
  123. public virtual string DataKeyField
  124. {
  125. get
  126. {
  127. object o = ViewState["DataKeyField"];
  128. if(o!=null)
  129. return (string)o;
  130. return String.Empty;
  131. }
  132. set
  133. {
  134. ViewState["DataKeyField"] = value;
  135. }
  136. }
  137. [Browsable(true)]
  138. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  139. [WebSysDescription("BaseDataList_DataKeys")]
  140. public DataKeyCollection DataKeys
  141. {
  142. get
  143. {
  144. if( dataKeys==null )
  145. dataKeys = new DataKeyCollection(DataKeysArray);
  146. return dataKeys;
  147. }
  148. }
  149. [DefaultValue("")]
  150. [WebCategory("Data")]
  151. [WebSysDescription("BaseDataList_DataMember")]
  152. public string DataMember
  153. {
  154. get
  155. {
  156. object o = ViewState["DataMember"];
  157. if(o!=null)
  158. return (string)o;
  159. return String.Empty;
  160. }
  161. set
  162. {
  163. ViewState["DataMember"] = value;
  164. }
  165. }
  166. [Bindable(true)]
  167. [DefaultValue(null)]
  168. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  169. [WebCategory("Data")]
  170. [WebSysDescription("BaseDataList_DataSource")]
  171. public virtual object DataSource {
  172. get {
  173. return dataSource;
  174. }
  175. set {
  176. if (value == null || value is IListSource || value is IEnumerable) {
  177. dataSource = value;
  178. } else {
  179. throw new ArgumentException (HttpRuntime.FormatResourceString (
  180. "Invalid_DataSource_Type", ID));
  181. }
  182. }
  183. }
  184. [Bindable(true)]
  185. [DefaultValue(GridLines.Both)]
  186. [WebCategory("Appearance")]
  187. [WebSysDescription("BaseDataList_GridLines")]
  188. public virtual GridLines GridLines
  189. {
  190. get
  191. {
  192. if(ControlStyleCreated)
  193. return ((TableStyle)ControlStyle).GridLines;
  194. return GridLines.Both;
  195. }
  196. set
  197. {
  198. ((TableStyle)ControlStyle).GridLines = value;
  199. }
  200. }
  201. // LAMESPEC HorizontalAlign has a Category attribute, this should obviously be a WebCategory attribute
  202. // but is defined incorrectly in the MS framework
  203. [Bindable(true)]
  204. [DefaultValue(HorizontalAlign.NotSet)]
  205. [Category("Layout")]
  206. [WebSysDescription("BaseDataList_HorizontalAlign")]
  207. public virtual HorizontalAlign HorizontalAlign
  208. {
  209. get
  210. {
  211. if(ControlStyleCreated)
  212. return ((TableStyle)ControlStyle).HorizontalAlign;
  213. return HorizontalAlign.NotSet;
  214. }
  215. set
  216. {
  217. ((TableStyle)ControlStyle).HorizontalAlign = value;
  218. }
  219. }
  220. protected ArrayList DataKeysArray
  221. {
  222. get
  223. {
  224. object o = ViewState["DataKeys"];
  225. if(o == null)
  226. {
  227. o = new ArrayList();
  228. ViewState["DataKeys"] = o;
  229. }
  230. return (ArrayList)o;
  231. }
  232. }
  233. protected override void AddParsedSubObject(object o)
  234. {
  235. // Preventing literal controls from being added as children.
  236. }
  237. protected override void CreateChildControls()
  238. {
  239. Controls.Clear();
  240. if(ViewState[ItemCountViewStateKey]!=null)
  241. {
  242. CreateControlHierarchy(false);
  243. ClearChildViewState();
  244. }
  245. }
  246. protected override void OnDataBinding(EventArgs e)
  247. {
  248. base.OnDataBinding(e);
  249. Controls.Clear();
  250. ClearChildViewState();
  251. CreateControlHierarchy(true);
  252. ChildControlsCreated = true;
  253. TrackViewState();
  254. }
  255. protected virtual void OnSelectedIndexChanged(EventArgs e)
  256. {
  257. if(Events != null)
  258. {
  259. EventHandler eh = (EventHandler)(Events[SelectedIndexChangedEvent]);
  260. if(eh!=null)
  261. eh(this, e);
  262. }
  263. }
  264. protected override void Render(HtmlTextWriter writer)
  265. {
  266. PrepareControlHierarchy();
  267. RenderContents(writer);
  268. }
  269. protected abstract void PrepareControlHierarchy();
  270. protected abstract void CreateControlHierarchy(bool useDataSource);
  271. #if NET_2_0
  272. // should be `internal protected' (why, oh WHY did they do that !?!)
  273. protected override void OnInit (EventArgs e)
  274. {
  275. base.OnInit(e);
  276. inited = true;
  277. if (!Page.IsPostBack)
  278. RequiresDataBinding = true;
  279. }
  280. // should be `internal protected' (why, oh WHY did they do that !?!)
  281. protected override void OnLoad (EventArgs e)
  282. {
  283. IDataSource ds = GetDataSourceObject () as IDataSource;
  284. if (ds != null && DataSourceID != "")
  285. ds.DataSourceChanged += new EventHandler (OnDataSourceChanged);
  286. base.OnLoad(e);
  287. }
  288. // should be `internal protected' (why, oh WHY did they do that !?!)
  289. protected override void OnPreRender (EventArgs e)
  290. {
  291. EnsureDataBound ();
  292. base.OnPreRender (e);
  293. }
  294. protected void EnsureDataBound ()
  295. {
  296. if (RequiresDataBinding && DataSourceID != "")
  297. DataBind ();
  298. }
  299. protected virtual object GetDataSourceObject ()
  300. {
  301. if (DataSourceID != "")
  302. return (IDataSource) NamingContainer.FindControl (DataSourceID);
  303. return DataSource;
  304. }
  305. protected virtual IEnumerable GetResolvedDataSource ()
  306. {
  307. if (DataSource != null && DataSourceID != "")
  308. throw new HttpException ();
  309. IDataSource ds = this.GetDataSourceObject () as IDataSource;
  310. if (ds != null && DataSourceID != "")
  311. return ds.GetView (DataMember).Select ();
  312. else if (DataSource != null)
  313. return DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
  314. else
  315. return null;
  316. }
  317. protected void OnDataSourceChanged (object sender, EventArgs e)
  318. {
  319. RequiresDataBinding = true;
  320. }
  321. public virtual string DataSourceID {
  322. get {
  323. object o = ViewState ["DataSourceID"];
  324. if (o != null)
  325. return (string)o;
  326. return String.Empty;
  327. }
  328. set {
  329. if (inited)
  330. RequiresDataBinding = true;
  331. ViewState ["DataSourceID"] = value;
  332. }
  333. }
  334. bool requiresDataBinding;
  335. protected bool RequiresDataBinding {
  336. get { return requiresDataBinding; }
  337. set { requiresDataBinding = value; }
  338. }
  339. protected bool inited;
  340. #else
  341. internal IEnumerable GetResolvedDataSource ()
  342. {
  343. if (DataSource != null)
  344. return DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
  345. else
  346. return null;
  347. }
  348. #endif
  349. }
  350. }