2 // System.Web.UI.WebControls.Repeater.cs
5 // Ben Maurer (bmaurer@novell.com)
7 // (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 // Helpful resources while implementing this class:
31 // _Developing Microsoft ASP.NET Server Controls and Components_ (Kothari, Datye)
32 // Chapters 16 and 20 (especially listing 20-3 on page 559)
34 // "Building DataBound Templated Custom ASP.NET Server Controls" (Mitchell) on MSDN
35 // Right now, http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/databoundtemplatedcontrols.asp
36 // works, but with msdn we all know that urls have a very short lifetime :-)
39 using System.Collections;
40 using System.ComponentModel;
41 using System.Security.Permissions;
42 using System.Web.Util;
44 namespace System.Web.UI.WebControls {
47 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
48 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
50 [DefaultEvent ("ItemCommand")]
51 [DefaultProperty ("DataSource")]
52 [Designer ("System.Web.UI.Design.WebControls.RepeaterDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
53 [ParseChildren (true)]
54 [PersistChildren (false)]
55 public class Repeater : Control, INamingContainer {
59 IDataSource boundDataSource;
60 private bool initialized;
61 private bool requiresDataBinding;
62 private DataSourceSelectArguments selectArguments;
63 private IEnumerable data;
66 // See Kothari, listing 20-3
72 override void CreateChildControls ()
74 // We are recreating the children from viewstate
77 // Build the children from the viewstate
78 if (ViewState ["Items"] != null)
79 CreateControlHierarchy (false);
82 // See Kothari, listing 20-3
83 protected override void OnDataBinding (EventArgs e)
85 base.OnDataBinding (EventArgs.Empty);
88 ClearChildViewState ();
91 CreateControlHierarchy (true);
93 ChildControlsCreated = true;
96 void DoItem (int i, ListItemType t, object d, bool databind)
98 RepeaterItem itm = CreateItem (i, t);
100 if (t == ListItemType.Item || t == ListItemType.AlternatingItem)
104 RepeaterItemEventArgs e = new RepeaterItemEventArgs (itm);
105 InitializeItem (itm);
108 // It is very important that this be called *before* data
109 // binding. Otherwise, we won't save our state in the viewstate.
120 protected virtual void CreateControlHierarchy (bool useDataSource)
123 items = new ArrayList ();
128 if (IsBoundUsingDataSourceID) {
133 ds = DataSourceResolver.ResolveDataSource (DataSource, DataMember);
136 // Optimize (shouldn't need all this memory ;-)
137 ds = new object [(int) ViewState ["Items"]];
140 // If there is no datasource, then we don't show anything. the "Items"
141 // viewstate won't get set, so on postback, we won't get here
145 if (HeaderTemplate != null)
146 DoItem (-1, ListItemType.Header, null, useDataSource);
149 foreach (object o in ds) {
150 if (idx != 0 && SeparatorTemplate != null)
151 DoItem (idx - 1, ListItemType.Separator, null, useDataSource);
153 DoItem (idx, idx % 2 == 0 ? ListItemType.Item : ListItemType.AlternatingItem, o, useDataSource);
157 if (FooterTemplate != null)
158 DoItem (-1, ListItemType.Footer, null, useDataSource);
160 ViewState ["Items"] = idx;
163 // Why does this get overriden?
164 public override void DataBind ()
166 // In all the examples I've seen online, this does base.OnDataBinding and
167 // then does all the create child controls stuff. But from stack traces on
168 // windows, this doesn't seem to be the case here.
169 OnDataBinding (EventArgs.Empty);
172 RequiresDataBinding = false;
176 protected virtual RepeaterItem CreateItem (int itemIndex, ListItemType itemType)
178 return new RepeaterItem (itemIndex, itemType);
181 protected virtual void InitializeItem (RepeaterItem item)
185 switch (item.ItemType) {
186 case ListItemType.Header:
189 case ListItemType.Footer:
192 case ListItemType.Item:
195 case ListItemType.AlternatingItem:
196 t = AlternatingItemTemplate;
200 case ListItemType.Separator:
201 t = SeparatorTemplate;
206 t.InstantiateIn (item);
210 protected override bool OnBubbleEvent (object sender, EventArgs e)
212 RepeaterCommandEventArgs rcea = e as RepeaterCommandEventArgs;
214 OnItemCommand (rcea);
222 public override ControlCollection Controls {
224 EnsureChildControls ();
225 return base.Controls;
230 RepeaterItemCollection itemscol;
233 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
234 [WebSysDescription ("")]
235 public virtual RepeaterItemCollection Items {
237 if (itemscol == null) {
239 items = new ArrayList ();
241 itemscol = new RepeaterItemCollection (items);
248 [WebSysDescription ("")]
249 [WebCategory ("Data")]
250 public virtual string DataMember {
252 return ViewState.GetString ("DataMember", "");
256 ViewState.Remove ("DataMember");
258 ViewState ["DataMember"] = value;
262 OnDataPropertyChanged ();
269 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
270 [WebSysDescription ("")]
271 [WebCategory ("Data")]
272 public virtual object DataSource {
278 if (value == null || value is IListSource || value is IEnumerable) {
280 // FIXME - can't duplicate in a test case ? LAMESPEC ?
281 // can't duplicate in a test case
282 // if ((dataSourceId != null) && (dataSourceId.Length != 0))
283 // throw new HttpException (Locale.GetText ("DataSourceID is already set."));
288 OnDataPropertyChanged ();
293 throw new ArgumentException (String.Format (
294 "An invalid data source is being used for {0}. A valid data source must implement either IListSource or IEnumerable",
301 [IDReferenceProperty (typeof (DataSourceControl))]
302 public virtual string DataSourceID
305 return ViewState.GetString ("DataSourceID", "");
308 if (dataSource != null)
309 throw new HttpException ("Only one of DataSource and DataSourceID can be specified.");
310 ViewState ["DataSourceID"] = value;
313 OnDataPropertyChanged ();
319 public virtual new bool EnableTheming
322 throw new NotImplementedException ();
325 throw new NotImplementedException ();
330 ITemplate alt_itm_tmpl;
333 [PersistenceMode(PersistenceMode.InnerProperty)]
334 [TemplateContainer (typeof (RepeaterItem))]
335 [WebSysDescription ("")]
336 public virtual ITemplate AlternatingItemTemplate {
341 alt_itm_tmpl = value;
345 ITemplate footer_tmpl;
348 [PersistenceMode(PersistenceMode.InnerProperty)]
349 [TemplateContainer (typeof (RepeaterItem))]
350 [WebSysDescription ("")]
351 public virtual ITemplate FooterTemplate {
360 ITemplate header_tmpl;
363 [PersistenceMode(PersistenceMode.InnerProperty)]
364 [TemplateContainer (typeof (RepeaterItem))]
365 [WebSysDescription ("")]
366 public virtual ITemplate HeaderTemplate {
378 [PersistenceMode(PersistenceMode.InnerProperty)]
379 [TemplateContainer (typeof (RepeaterItem))]
380 [WebSysDescription ("")]
381 public virtual ITemplate ItemTemplate {
390 ITemplate separator_tmpl;
393 [PersistenceMode(PersistenceMode.InnerProperty)]
394 [TemplateContainer (typeof (RepeaterItem))]
395 [WebSysDescription ("")]
396 public virtual ITemplate SeparatorTemplate {
398 return separator_tmpl;
401 separator_tmpl = value;
406 protected virtual void OnItemCommand (RepeaterCommandEventArgs e)
408 RepeaterCommandEventHandler h = (RepeaterCommandEventHandler) Events [ItemCommandEvent];
413 static readonly object ItemCommandEvent = new object ();
415 [WebSysDescription ("")]
416 [WebCategory ("Action")]
417 public event RepeaterCommandEventHandler ItemCommand {
418 add { Events.AddHandler (ItemCommandEvent, value); }
419 remove { Events.RemoveHandler (ItemCommandEvent, value); }
423 protected virtual void OnItemCreated (RepeaterItemEventArgs e)
425 RepeaterItemEventHandler h = (RepeaterItemEventHandler) Events [ItemCreatedEvent];
430 static readonly object ItemCreatedEvent = new object ();
432 [WebSysDescription ("")]
433 [WebCategory ("Behavior")]
434 public event RepeaterItemEventHandler ItemCreated {
435 add { Events.AddHandler (ItemCreatedEvent, value); }
436 remove { Events.RemoveHandler (ItemCreatedEvent, value); }
439 protected virtual void OnItemDataBound (RepeaterItemEventArgs e)
441 RepeaterItemEventHandler h = (RepeaterItemEventHandler) Events [ItemDataBoundEvent];
446 static readonly object ItemDataBoundEvent = new object ();
448 [WebSysDescription ("")]
449 [WebCategory ("Behavior")]
450 public event RepeaterItemEventHandler ItemDataBound {
451 add { Events.AddHandler (ItemDataBoundEvent, value); }
452 remove { Events.RemoveHandler (ItemDataBoundEvent, value); }
456 protected bool Initialized {
457 get { return initialized; }
460 protected bool IsBoundUsingDataSourceID
462 get { return (DataSourceID.Length != 0); }
465 protected bool RequiresDataBinding
467 get { return requiresDataBinding; }
468 set { requiresDataBinding = value; }
471 protected DataSourceSelectArguments SelectArguments
474 /* i know this seems weird - i mean, why
476 * CreateDataSourceSelectArguments here? i
477 * have no idea. ask MS */
478 if (selectArguments == null)
479 selectArguments = new DataSourceSelectArguments ();
480 return selectArguments;
484 protected virtual DataSourceSelectArguments CreateDataSourceSelectArguments ()
486 if (selectArguments == null)
487 selectArguments = new DataSourceSelectArguments ();
488 return selectArguments;
491 protected void EnsureDataBound ()
493 if (IsBoundUsingDataSourceID && RequiresDataBinding)
497 private void SelectCallback (IEnumerable data)
502 protected virtual IEnumerable GetData ()
505 if (DataSourceID.Length == 0)
508 if (boundDataSource == null)
511 DataSourceView dsv = boundDataSource.GetView (String.Empty);
512 dsv.Select (SelectArguments, new DataSourceViewSelectCallback (SelectCallback));
520 protected virtual void OnDataPropertyChanged ()
525 protected virtual void OnDataSourceViewChanged (object sender, EventArgs e)
527 RequiresDataBinding = true;
531 protected internal override void OnInit (EventArgs e)
536 protected internal override void OnLoad (EventArgs e)
538 if ((Page != null) && !Page.IsPostBack)
539 RequiresDataBinding = true;
544 if (IsBoundUsingDataSourceID)
545 ConnectToDataSource ();
548 protected internal override void OnPreRender (EventArgs e)
551 base.OnPreRender (e);
554 void ConnectToDataSource ()
556 /* verify that the data source exists and is an IDataSource */
559 ctrl = Parent.FindControl (DataSourceID);
561 if (ctrl == null || !(ctrl is IDataSource)) {
565 format = "DataSourceID of '{0}' must be the ID of a control of type IDataSource. A control with ID '{1}' could not be found.";
567 format = "DataSourceID of '{0}' must be the ID of a control of type IDataSource. '{1}' is not an IDataSource.";
569 throw new HttpException (String.Format (format, ID, DataSourceID));
572 boundDataSource = (IDataSource)ctrl;
573 boundDataSource.GetView (String.Empty).DataSourceViewChanged += new EventHandler(OnDataSourceViewChanged);