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
58 IDataSource boundDataSource;
60 bool preRendered = false;
61 bool requiresDataBinding;
62 DataSourceSelectArguments selectArguments;
65 // See Kothari, listing 20-3
67 override void CreateChildControls ()
69 // We are recreating the children from viewstate
72 // Build the children from the viewstate
73 if (ViewState ["Items"] != null)
74 CreateControlHierarchy (false);
77 // See Kothari, listing 20-3
78 protected override void OnDataBinding (EventArgs e)
80 base.OnDataBinding (EventArgs.Empty);
83 ClearChildViewState ();
86 CreateControlHierarchy (true);
88 ChildControlsCreated = true;
91 void DoItem (int i, ListItemType t, object d, bool databind)
93 RepeaterItem itm = CreateItem (i, t);
95 if (t == ListItemType.Item || t == ListItemType.AlternatingItem)
99 RepeaterItemEventArgs e = new RepeaterItemEventArgs (itm);
100 InitializeItem (itm);
103 // It is very important that this be called *before* data
104 // binding. Otherwise, we won't save our state in the viewstate.
115 protected virtual void CreateControlHierarchy (bool useDataSource)
118 items = new ArrayList ();
125 // Optimize (shouldn't need all this memory ;-)
126 ds = new object [(int) ViewState ["Items"]];
129 // If there is no datasource, then we don't show anything. the "Items"
130 // viewstate won't get set, so on postback, we won't get here
134 if (HeaderTemplate != null)
135 DoItem (-1, ListItemType.Header, null, useDataSource);
138 foreach (object o in ds) {
139 if (idx != 0 && SeparatorTemplate != null)
140 DoItem (idx - 1, ListItemType.Separator, null, useDataSource);
142 DoItem (idx, idx % 2 == 0 ? ListItemType.Item : ListItemType.AlternatingItem, o, useDataSource);
146 if (FooterTemplate != null)
147 DoItem (-1, ListItemType.Footer, null, useDataSource);
149 ViewState ["Items"] = idx;
152 // Why does this get overriden?
153 public override void DataBind ()
155 // In all the examples I've seen online, this does base.OnDataBinding and
156 // then does all the create child controls stuff. But from stack traces on
157 // windows, this doesn't seem to be the case here.
158 OnDataBinding (EventArgs.Empty);
160 RequiresDataBinding = false;
163 protected virtual RepeaterItem CreateItem (int itemIndex, ListItemType itemType)
165 return new RepeaterItem (itemIndex, itemType);
168 protected virtual void InitializeItem (RepeaterItem item)
172 switch (item.ItemType) {
173 case ListItemType.Header:
176 case ListItemType.Footer:
179 case ListItemType.Item:
182 case ListItemType.AlternatingItem:
183 t = AlternatingItemTemplate;
187 case ListItemType.Separator:
188 t = SeparatorTemplate;
193 t.InstantiateIn (item);
197 protected override bool OnBubbleEvent (object sender, EventArgs e)
199 RepeaterCommandEventArgs rcea = e as RepeaterCommandEventArgs;
201 OnItemCommand (rcea);
209 public override ControlCollection Controls {
211 EnsureChildControls ();
212 return base.Controls;
217 RepeaterItemCollection itemscol;
220 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
221 [WebSysDescription ("")]
222 public virtual RepeaterItemCollection Items {
224 if (itemscol == null) {
226 items = new ArrayList ();
228 itemscol = new RepeaterItemCollection (items);
235 [WebSysDescription ("")]
236 [WebCategory ("Data")]
237 public virtual string DataMember {
239 return ViewState.GetString ("DataMember", "");
243 ViewState.Remove ("DataMember");
245 ViewState ["DataMember"] = value;
248 OnDataPropertyChanged ();
254 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
255 [WebSysDescription ("")]
256 [WebCategory ("Data")]
257 public virtual object DataSource {
263 if (value == null || value is IListSource || value is IEnumerable) {
264 // FIXME - can't duplicate in a test case ? LAMESPEC ?
265 // can't duplicate in a test case
266 // if ((dataSourceId != null) && (dataSourceId.Length != 0))
267 // throw new HttpException (Locale.GetText ("DataSourceID is already set."));
272 OnDataPropertyChanged ();
274 throw new ArgumentException (String.Format (
275 "An invalid data source is being used for {0}. A valid data source must implement either IListSource or IEnumerable",
281 [IDReferenceProperty (typeof (DataSourceControl))]
282 public virtual string DataSourceID
285 return ViewState.GetString ("DataSourceID", "");
288 if (dataSource != null)
289 throw new HttpException ("Only one of DataSource and DataSourceID can be specified.");
290 ViewState ["DataSourceID"] = value;
293 OnDataPropertyChanged ();
298 public override bool EnableTheming {
299 get { return base.EnableTheming; }
300 set { base.EnableTheming = value; }
303 ITemplate alt_itm_tmpl;
306 [PersistenceMode(PersistenceMode.InnerProperty)]
307 [TemplateContainer (typeof (RepeaterItem))]
308 [WebSysDescription ("")]
309 public virtual ITemplate AlternatingItemTemplate {
314 alt_itm_tmpl = value;
318 ITemplate footer_tmpl;
321 [PersistenceMode(PersistenceMode.InnerProperty)]
322 [TemplateContainer (typeof (RepeaterItem))]
323 [WebSysDescription ("")]
324 public virtual ITemplate FooterTemplate {
333 ITemplate header_tmpl;
336 [PersistenceMode(PersistenceMode.InnerProperty)]
337 [TemplateContainer (typeof (RepeaterItem))]
338 [WebSysDescription ("")]
339 public virtual ITemplate HeaderTemplate {
351 [PersistenceMode(PersistenceMode.InnerProperty)]
352 [TemplateContainer (typeof (RepeaterItem))]
353 [WebSysDescription ("")]
354 public virtual ITemplate ItemTemplate {
363 ITemplate separator_tmpl;
366 [PersistenceMode(PersistenceMode.InnerProperty)]
367 [TemplateContainer (typeof (RepeaterItem))]
368 [WebSysDescription ("")]
369 public virtual ITemplate SeparatorTemplate {
371 return separator_tmpl;
374 separator_tmpl = value;
379 protected virtual void OnItemCommand (RepeaterCommandEventArgs e)
381 RepeaterCommandEventHandler h = (RepeaterCommandEventHandler) Events [ItemCommandEvent];
386 static readonly object ItemCommandEvent = new object ();
388 [WebSysDescription ("")]
389 [WebCategory ("Action")]
390 public event RepeaterCommandEventHandler ItemCommand {
391 add { Events.AddHandler (ItemCommandEvent, value); }
392 remove { Events.RemoveHandler (ItemCommandEvent, value); }
396 protected virtual void OnItemCreated (RepeaterItemEventArgs e)
398 RepeaterItemEventHandler h = (RepeaterItemEventHandler) Events [ItemCreatedEvent];
403 static readonly object ItemCreatedEvent = new object ();
405 [WebSysDescription ("")]
406 [WebCategory ("Behavior")]
407 public event RepeaterItemEventHandler ItemCreated {
408 add { Events.AddHandler (ItemCreatedEvent, value); }
409 remove { Events.RemoveHandler (ItemCreatedEvent, value); }
412 protected virtual void OnItemDataBound (RepeaterItemEventArgs e)
414 RepeaterItemEventHandler h = (RepeaterItemEventHandler) Events [ItemDataBoundEvent];
419 static readonly object ItemDataBoundEvent = new object ();
421 [WebSysDescription ("")]
422 [WebCategory ("Behavior")]
423 public event RepeaterItemEventHandler ItemDataBound {
424 add { Events.AddHandler (ItemDataBoundEvent, value); }
425 remove { Events.RemoveHandler (ItemDataBoundEvent, value); }
428 protected bool Initialized {
429 get { return initialized; }
432 protected bool IsBoundUsingDataSourceID
434 get { return (DataSourceID.Length != 0); }
437 protected bool RequiresDataBinding
439 get { return requiresDataBinding; }
441 requiresDataBinding = value;
442 if (value && preRendered && IsBoundUsingDataSourceID && Page != null && !Page.IsCallback)
447 protected DataSourceSelectArguments SelectArguments
450 // MSDN: The first call to the SelectArguments property calls the
451 // CreateDataSourceSelectArguments method to return the Empty value.
452 if (selectArguments == null)
453 selectArguments = CreateDataSourceSelectArguments();
454 return selectArguments;
458 protected virtual DataSourceSelectArguments CreateDataSourceSelectArguments ()
460 // MSDN: Returns the Empty value.
461 return DataSourceSelectArguments.Empty;
464 protected void EnsureDataBound ()
466 if (IsBoundUsingDataSourceID && RequiresDataBinding)
470 void SelectCallback (IEnumerable data)
476 IEnumerable GetData ()
479 if (IsBoundUsingDataSourceID) {
480 if (DataSourceID.Length == 0)
483 if (boundDataSource == null)
486 DataSourceView dsv = boundDataSource.GetView (String.Empty);
487 dsv.Select (SelectArguments, new DataSourceViewSelectCallback (SelectCallback));
493 result = DataSourceResolver.ResolveDataSource (DataSource, DataMember);
498 protected virtual void OnDataPropertyChanged ()
501 RequiresDataBinding = true;
504 protected virtual void OnDataSourceViewChanged (object sender, EventArgs e)
506 RequiresDataBinding = true;
509 protected internal override void OnInit (EventArgs e)
514 page.PreLoad += new EventHandler (OnPagePreLoad);
516 if (!IsViewStateEnabled && page.IsPostBack)
517 RequiresDataBinding = true;
521 void OnPagePreLoad (object sender, EventArgs e)
526 protected internal override void OnLoad (EventArgs e)
538 if (!page.IsPostBack || (IsViewStateEnabled && (ViewState ["Items"] == null)))
539 RequiresDataBinding = true;
542 if (IsBoundUsingDataSourceID)
543 ConnectToDataSource ();
548 protected internal override void OnPreRender (EventArgs e)
552 base.OnPreRender (e);
555 void ConnectToDataSource ()
557 /* verify that the data source exists and is an IDataSource */
560 ctrl = Parent.FindControl (DataSourceID);
562 if (ctrl == null || !(ctrl is IDataSource)) {
566 format = "DataSourceID of '{0}' must be the ID of a control of type IDataSource. A control with ID '{1}' could not be found.";
568 format = "DataSourceID of '{0}' must be the ID of a control of type IDataSource. '{1}' is not an IDataSource.";
570 throw new HttpException (String.Format (format, ID, DataSourceID));
573 boundDataSource = (IDataSource)ctrl;
574 boundDataSource.GetView (String.Empty).DataSourceViewChanged += new EventHandler(OnDataSourceViewChanged);