Repeater.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. //
  2. // System.Web.UI.WebControls.Repeater.cs
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. // Helpful resources while implementing this class:
  29. //
  30. // _Developing Microsoft ASP.NET Server Controls and Components_ (Kothari, Datye)
  31. // Chapters 16 and 20 (especially listing 20-3 on page 559)
  32. //
  33. // "Building DataBound Templated Custom ASP.NET Server Controls" (Mitchell) on MSDN
  34. // Right now, http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/databoundtemplatedcontrols.asp
  35. // works, but with msdn we all know that urls have a very short lifetime :-)
  36. //
  37. using System.Collections;
  38. using System.ComponentModel;
  39. using System.Security.Permissions;
  40. using System.Web.Util;
  41. namespace System.Web.UI.WebControls {
  42. // CAS
  43. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  44. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  45. // attributes
  46. [DefaultEvent ("ItemCommand")]
  47. [DefaultProperty ("DataSource")]
  48. [Designer ("System.Web.UI.Design.WebControls.RepeaterDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  49. [ParseChildren (true)]
  50. [PersistChildren (false)]
  51. public class Repeater : Control, INamingContainer {
  52. object dataSource;
  53. #if NET_2_0
  54. IDataSource boundDataSource;
  55. private bool initialized;
  56. private bool requiresDataBinding;
  57. private DataSourceSelectArguments selectArguments;
  58. private IEnumerable data;
  59. #endif
  60. // See Kothari, listing 20-3
  61. #if NET_2_0
  62. protected internal
  63. #else
  64. protected
  65. #endif
  66. override void CreateChildControls ()
  67. {
  68. // We are recreating the children from viewstate
  69. Controls.Clear();
  70. // Build the children from the viewstate
  71. if (ViewState ["Items"] != null)
  72. CreateControlHierarchy (false);
  73. }
  74. // See Kothari, listing 20-3
  75. protected override void OnDataBinding (EventArgs e)
  76. {
  77. base.OnDataBinding (EventArgs.Empty);
  78. Controls.Clear ();
  79. ClearChildViewState ();
  80. TrackViewState ();
  81. CreateControlHierarchy (true);
  82. ChildControlsCreated = true;
  83. }
  84. void DoItem (int i, ListItemType t, object d, bool databind)
  85. {
  86. RepeaterItem itm = CreateItem (i, t);
  87. if (t == ListItemType.Item || t == ListItemType.AlternatingItem)
  88. items.Add (itm);
  89. itm.DataItem = d;
  90. RepeaterItemEventArgs e = new RepeaterItemEventArgs (itm);
  91. InitializeItem (itm);
  92. //
  93. // It is very important that this be called *before* data
  94. // binding. Otherwise, we won't save our state in the viewstate.
  95. //
  96. Controls.Add (itm);
  97. OnItemCreated (e);
  98. if (databind) {
  99. itm.DataBind ();
  100. OnItemDataBound (e);
  101. }
  102. }
  103. protected virtual void CreateControlHierarchy (bool useDataSource)
  104. {
  105. IEnumerable ds;
  106. items = new ArrayList ();
  107. itemscol = null;
  108. if (useDataSource) {
  109. #if NET_2_0
  110. if (IsBoundUsingDataSourceID) {
  111. ds = GetData ();
  112. }
  113. else
  114. #endif
  115. ds = DataSourceResolver.ResolveDataSource (DataSource, DataMember);
  116. }
  117. else {
  118. // Optimize (shouldn't need all this memory ;-)
  119. ds = new object [(int) ViewState ["Items"]];
  120. }
  121. // If there is no datasource, then we don't show anything. the "Items"
  122. // viewstate won't get set, so on postback, we won't get here
  123. if (ds == null)
  124. return;
  125. if (HeaderTemplate != null)
  126. DoItem (-1, ListItemType.Header, null, useDataSource);
  127. int idx = 0;
  128. foreach (object o in ds) {
  129. if (idx != 0 && SeparatorTemplate != null)
  130. DoItem (idx - 1, ListItemType.Separator, null, useDataSource);
  131. DoItem (idx, idx % 2 == 0 ? ListItemType.Item : ListItemType.AlternatingItem, o, useDataSource);
  132. idx ++;
  133. }
  134. if (FooterTemplate != null)
  135. DoItem (-1, ListItemType.Footer, null, useDataSource);
  136. ViewState ["Items"] = idx;
  137. }
  138. // Why does this get overriden?
  139. public override void DataBind ()
  140. {
  141. // In all the examples I've seen online, this does base.OnDataBinding and
  142. // then does all the create child controls stuff. But from stack traces on
  143. // windows, this doesn't seem to be the case here.
  144. OnDataBinding (EventArgs.Empty);
  145. #if NET_2_0
  146. RequiresDataBinding = false;
  147. #endif
  148. }
  149. protected virtual RepeaterItem CreateItem (int itemIndex, ListItemType itemType)
  150. {
  151. return new RepeaterItem (itemIndex, itemType);
  152. }
  153. protected virtual void InitializeItem (RepeaterItem item)
  154. {
  155. ITemplate t = null;
  156. switch (item.ItemType) {
  157. case ListItemType.Header:
  158. t = HeaderTemplate;
  159. break;
  160. case ListItemType.Footer:
  161. t = FooterTemplate;
  162. break;
  163. case ListItemType.Item:
  164. t = ItemTemplate;
  165. break;
  166. case ListItemType.AlternatingItem:
  167. t = AlternatingItemTemplate;
  168. if (t == null)
  169. t = ItemTemplate;
  170. break;
  171. case ListItemType.Separator:
  172. t = SeparatorTemplate;
  173. break;
  174. }
  175. if (t != null)
  176. t.InstantiateIn (item);
  177. }
  178. protected override bool OnBubbleEvent (object sender, EventArgs e)
  179. {
  180. RepeaterCommandEventArgs rcea = e as RepeaterCommandEventArgs;
  181. if (rcea != null) {
  182. OnItemCommand (rcea);
  183. return true;
  184. }
  185. return false;
  186. }
  187. public override ControlCollection Controls {
  188. get {
  189. EnsureChildControls ();
  190. return base.Controls;
  191. }
  192. }
  193. RepeaterItemCollection itemscol;
  194. ArrayList items;
  195. [Browsable(false)]
  196. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  197. [WebSysDescription ("")]
  198. public virtual RepeaterItemCollection Items {
  199. get {
  200. if (itemscol == null)
  201. itemscol = new RepeaterItemCollection (items);
  202. return itemscol;
  203. }
  204. }
  205. [DefaultValue("")]
  206. [WebSysDescription ("")]
  207. [WebCategory ("Data")]
  208. public virtual string DataMember {
  209. get {
  210. return ViewState.GetString ("DataMember", "");
  211. }
  212. set {
  213. if (value == null)
  214. ViewState.Remove ("DataMember");
  215. else
  216. ViewState ["DataMember"] = value;
  217. #if NET_2_0
  218. if (!Initialized)
  219. OnDataPropertyChanged ();
  220. #endif
  221. }
  222. }
  223. [Bindable(true)]
  224. [DefaultValue(null)]
  225. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  226. [WebSysDescription ("")]
  227. [WebCategory ("Data")]
  228. public virtual object DataSource {
  229. get {
  230. return dataSource;
  231. }
  232. set {
  233. if (value == null || value is IListSource || value is IEnumerable) {
  234. #if NET_2_0
  235. // FIXME - can't duplicate in a test case ? LAMESPEC ?
  236. // can't duplicate in a test case
  237. // if ((dataSourceId != null) && (dataSourceId.Length != 0))
  238. // throw new HttpException (Locale.GetText ("DataSourceID is already set."));
  239. dataSource = value;
  240. if (!Initialized)
  241. OnDataPropertyChanged ();
  242. #else
  243. dataSource = value;
  244. #endif
  245. } else
  246. throw new ArgumentException (String.Format (
  247. "An invalid data source is being used for {0}. A valid data source must implement either IListSource or IEnumerable",
  248. ID));
  249. }
  250. }
  251. #if NET_2_0
  252. [DefaultValue ("")]
  253. [IDReferenceProperty (typeof (DataSourceControl))]
  254. public virtual string DataSourceID
  255. {
  256. get {
  257. return ViewState.GetString ("DataSourceID", "");
  258. }
  259. set {
  260. if (dataSource != null)
  261. throw new HttpException ("Only one of DataSource and DataSourceID can be specified.");
  262. ViewState ["DataSourceID"] = value;
  263. if (!Initialized)
  264. OnDataPropertyChanged ();
  265. }
  266. }
  267. [Browsable (true)]
  268. [MonoTODO]
  269. public virtual new bool EnableTheming
  270. {
  271. get {
  272. throw new NotImplementedException ();
  273. }
  274. set {
  275. throw new NotImplementedException ();
  276. }
  277. }
  278. #endif
  279. ITemplate alt_itm_tmpl;
  280. [Browsable(false)]
  281. [DefaultValue(null)]
  282. [PersistenceMode(PersistenceMode.InnerProperty)]
  283. [TemplateContainer (typeof (RepeaterItem))]
  284. [WebSysDescription ("")]
  285. public virtual ITemplate AlternatingItemTemplate {
  286. get {
  287. return alt_itm_tmpl;
  288. }
  289. set {
  290. alt_itm_tmpl = value;
  291. }
  292. }
  293. ITemplate footer_tmpl;
  294. [Browsable(false)]
  295. [DefaultValue(null)]
  296. [PersistenceMode(PersistenceMode.InnerProperty)]
  297. [TemplateContainer (typeof (RepeaterItem))]
  298. [WebSysDescription ("")]
  299. public virtual ITemplate FooterTemplate {
  300. get {
  301. return footer_tmpl;
  302. }
  303. set {
  304. footer_tmpl = value;
  305. }
  306. }
  307. ITemplate header_tmpl;
  308. [Browsable(false)]
  309. [DefaultValue(null)]
  310. [PersistenceMode(PersistenceMode.InnerProperty)]
  311. [TemplateContainer (typeof (RepeaterItem))]
  312. [WebSysDescription ("")]
  313. public virtual ITemplate HeaderTemplate {
  314. get {
  315. return header_tmpl;
  316. }
  317. set {
  318. header_tmpl = value;
  319. }
  320. }
  321. ITemplate item_tmpl;
  322. [Browsable(false)]
  323. [DefaultValue(null)]
  324. [PersistenceMode(PersistenceMode.InnerProperty)]
  325. [TemplateContainer (typeof (RepeaterItem))]
  326. [WebSysDescription ("")]
  327. public virtual ITemplate ItemTemplate {
  328. get {
  329. return item_tmpl;
  330. }
  331. set {
  332. item_tmpl = value;
  333. }
  334. }
  335. ITemplate separator_tmpl;
  336. [Browsable(false)]
  337. [DefaultValue(null)]
  338. [PersistenceMode(PersistenceMode.InnerProperty)]
  339. [TemplateContainer (typeof (RepeaterItem))]
  340. [WebSysDescription ("")]
  341. public virtual ITemplate SeparatorTemplate {
  342. get {
  343. return separator_tmpl;
  344. }
  345. set {
  346. separator_tmpl = value;
  347. }
  348. }
  349. protected virtual void OnItemCommand (RepeaterCommandEventArgs e)
  350. {
  351. RepeaterCommandEventHandler h = (RepeaterCommandEventHandler) Events [ItemCommandEvent];
  352. if (h != null)
  353. h (this, e);
  354. }
  355. static readonly object ItemCommandEvent = new object ();
  356. [WebSysDescription ("")]
  357. [WebCategory ("Action")]
  358. public event RepeaterCommandEventHandler ItemCommand {
  359. add { Events.AddHandler (ItemCommandEvent, value); }
  360. remove { Events.RemoveHandler (ItemCommandEvent, value); }
  361. }
  362. protected virtual void OnItemCreated (RepeaterItemEventArgs e)
  363. {
  364. RepeaterItemEventHandler h = (RepeaterItemEventHandler) Events [ItemCreatedEvent];
  365. if (h != null)
  366. h (this, e);
  367. }
  368. static readonly object ItemCreatedEvent = new object ();
  369. [WebSysDescription ("")]
  370. [WebCategory ("Behavior")]
  371. public event RepeaterItemEventHandler ItemCreated {
  372. add { Events.AddHandler (ItemCreatedEvent, value); }
  373. remove { Events.RemoveHandler (ItemCreatedEvent, value); }
  374. }
  375. protected virtual void OnItemDataBound (RepeaterItemEventArgs e)
  376. {
  377. RepeaterItemEventHandler h = (RepeaterItemEventHandler) Events [ItemDataBoundEvent];
  378. if (h != null)
  379. h (this, e);
  380. }
  381. static readonly object ItemDataBoundEvent = new object ();
  382. [WebSysDescription ("")]
  383. [WebCategory ("Behavior")]
  384. public event RepeaterItemEventHandler ItemDataBound {
  385. add { Events.AddHandler (ItemDataBoundEvent, value); }
  386. remove { Events.RemoveHandler (ItemDataBoundEvent, value); }
  387. }
  388. #if NET_2_0
  389. protected bool Initialized {
  390. get { return initialized; }
  391. }
  392. protected bool IsBoundUsingDataSourceID
  393. {
  394. get { return (DataSourceID.Length != 0); }
  395. }
  396. protected bool RequiresDataBinding
  397. {
  398. get { return requiresDataBinding; }
  399. set { requiresDataBinding = value; }
  400. }
  401. protected DataSourceSelectArguments SelectArguments
  402. {
  403. get {
  404. /* i know this seems weird - i mean, why
  405. * don't we call
  406. * CreateDataSourceSelectArguments here? i
  407. * have no idea. ask MS */
  408. if (selectArguments == null)
  409. selectArguments = new DataSourceSelectArguments ();
  410. return selectArguments;
  411. }
  412. }
  413. protected virtual DataSourceSelectArguments CreateDataSourceSelectArguments ()
  414. {
  415. if (selectArguments == null)
  416. selectArguments = new DataSourceSelectArguments ();
  417. return selectArguments;
  418. }
  419. protected void EnsureDataBound ()
  420. {
  421. if (IsBoundUsingDataSourceID && RequiresDataBinding)
  422. DataBind ();
  423. }
  424. private void SelectCallback (IEnumerable data)
  425. {
  426. this.data = data;
  427. }
  428. protected virtual IEnumerable GetData ()
  429. {
  430. IEnumerable result;
  431. if (DataSourceID.Length == 0)
  432. return null;
  433. if (boundDataSource == null)
  434. return null;
  435. DataSourceView dsv = boundDataSource.GetView (String.Empty);
  436. dsv.Select (SelectArguments, new DataSourceViewSelectCallback (SelectCallback));
  437. result = data;
  438. data = null;
  439. return result;
  440. }
  441. [MonoTODO]
  442. protected virtual void OnDataPropertyChanged ()
  443. {
  444. }
  445. [MonoTODO]
  446. protected virtual void OnDataSourceViewChanged (object sender, EventArgs e)
  447. {
  448. RequiresDataBinding = true;
  449. }
  450. [MonoTODO]
  451. protected internal override void OnInit (EventArgs e)
  452. {
  453. base.OnInit (e);
  454. }
  455. protected internal override void OnLoad (EventArgs e)
  456. {
  457. if ((Page != null) && !Page.IsPostBack)
  458. RequiresDataBinding = true;
  459. initialized = true;
  460. base.OnLoad (e);
  461. if (IsBoundUsingDataSourceID)
  462. ConnectToDataSource ();
  463. }
  464. protected internal override void OnPreRender (EventArgs e)
  465. {
  466. EnsureDataBound ();
  467. base.OnPreRender (e);
  468. }
  469. void ConnectToDataSource ()
  470. {
  471. /* verify that the data source exists and is an IDataSource */
  472. object ctrl = null;
  473. if (Parent != null)
  474. ctrl = Parent.FindControl (DataSourceID);
  475. if (ctrl == null || !(ctrl is IDataSource)) {
  476. string format;
  477. if (ctrl == null)
  478. format = "DataSourceID of '{0}' must be the ID of a control of type IDataSource. A control with ID '{1}' could not be found.";
  479. else
  480. format = "DataSourceID of '{0}' must be the ID of a control of type IDataSource. '{1}' is not an IDataSource.";
  481. throw new HttpException (String.Format (format, ID, DataSourceID));
  482. }
  483. boundDataSource = (IDataSource)ctrl;
  484. boundDataSource.GetView (String.Empty).DataSourceViewChanged += new EventHandler(OnDataSourceViewChanged);
  485. }
  486. #endif
  487. }
  488. }