File: List.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (623 lines) | stat: -rw-r--r-- 21,595 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
//------------------------------------------------------------------------------
// <copyright file="List.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>                                                                
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Design;
using System.Globalization;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Security.Permissions;

namespace System.Web.UI.MobileControls
{
    /*
     * Mobile List class.
     *
     * Copyright (c) 2000 Microsoft Corporation
     */
    /// <include file='doc\List.uex' path='docs/doc[@for="List"]/*' />
    [
        ControlBuilderAttribute(typeof(ListControlBuilder)),
        DefaultEvent("ItemCommand"),
        DefaultProperty("DataSource"),
        Designer(typeof(System.Web.UI.Design.MobileControls.ListDesigner)),
        DesignerAdapter(typeof(System.Web.UI.Design.MobileControls.Adapters.DesignerListAdapter)),
        Editor(typeof(System.Web.UI.Design.MobileControls.ListComponentEditor), typeof(ComponentEditor)),
        ToolboxData("<{0}:List runat=\"server\"></{0}:List>"),
        ToolboxItem("System.Web.UI.Design.WebControlToolboxItem, " + AssemblyRef.SystemDesign),
    ]
    [AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal)]
    [Obsolete("The System.Web.Mobile.dll assembly has been deprecated and should no longer be used. For information about how to develop ASP.NET mobile applications, see http://go.microsoft.com/fwlink/?LinkId=157231.")]
    public class List : PagedControl, INamingContainer, IListControl, ITemplateable, IPostBackEventHandler
    {
        private static readonly Object EventItemCommand = new Object();
        private static readonly Object EventItemDataBind = new Object();

        private ListDataHelper _dataHelper;
        private ListDecoration _decoration = ListDecoration.None;

        private ListDataHelper DataHelper {
            get {
                if (_dataHelper == null) {
                    _dataHelper = new ListDataHelper(this, ViewState);
                }

                return _dataHelper;
            }
        }

        /// <include file='doc\List.uex' path='docs/doc[@for="List.DataSource"]/*' />
        /// <summary>
        /// <para>
        /// Gets or sets the <see langword='DataSource'/> property of the control which is used to populate
        /// the items within the control.
        /// </para>
        /// </summary>
        [
            Bindable(true),
            DefaultValue(null),
            DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
            MobileCategory(SR.Category_Data),
            MobileSysDescription(SR.List_DataSource)
        ]
        public virtual Object DataSource 
        {
            get 
            {
                return DataHelper.DataSource;
            }

            set 
            {
                DataHelper.DataSource = value;
            }
        }

        /// <include file='doc\List.uex' path='docs/doc[@for="List.DataMember"]/*' />
        [
            Bindable(false),
            DefaultValue(""),
            MobileCategory(SR.Category_Data),
            MobileSysDescription(SR.List_DataMember),
            TypeConverter(typeof(System.Web.UI.Design.MobileControls.Converters.DataMemberConverter))
        ]
        public virtual String DataMember
        {
            get 
            {
                return DataHelper.DataMember;
            }

            set 
            {
                DataHelper.DataMember = value;
            }
        }

        /// <include file='doc\List.uex' path='docs/doc[@for="List.DataTextField"]/*' />
        [
            DefaultValue(""),
            MobileCategory(SR.Category_Data),
            MobileSysDescription(SR.List_DataTextField),
            TypeConverter(typeof(System.Web.UI.Design.MobileControls.Converters.DataFieldConverter))
        ]
        public String DataTextField 
        {
            get 
            {
                return DataHelper.DataTextField;
            }
            set 
            {
                DataHelper.DataTextField = value;
            }
        }

        /// <include file='doc\List.uex' path='docs/doc[@for="List.DataValueField"]/*' />
        [
            DefaultValue(""),
            MobileCategory(SR.Category_Data),
            MobileSysDescription(SR.List_DataValueField),
            TypeConverter(typeof(System.Web.UI.Design.MobileControls.Converters.DataFieldConverter))
        ]
        public String DataValueField 
        {
            get 
            {
                return DataHelper.DataValueField;
            }
            set 
            {
                DataHelper.DataValueField = value;
            }
        }


        /// <include file='doc\List.uex' path='docs/doc[@for="List.ItemsAsLinks"]/*' />
        [
            Bindable(true),
            DefaultValue(false),
            MobileCategory(SR.Category_Behavior),
            MobileSysDescription(SR.List_ItemsAsLinks)
        ]
        public bool ItemsAsLinks
        {
            get
            {
                Object b = ViewState["ItemsAsLinks"];
                return (b != null) ? (bool) b : false;
            }
            set
            {
                ViewState["ItemsAsLinks"] = value;
            }
        }

        /// <include file='doc\List.uex' path='docs/doc[@for="List.Items"]/*' />
        [
            Bindable(false),
            DefaultValue(null),
            Editor(typeof(System.Web.UI.Design.MobileControls.ItemCollectionEditor), typeof(UITypeEditor)),
            MergableProperty(false),
            MobileSysDescription(SR.List_Items),
            PersistenceMode(PersistenceMode.InnerDefaultProperty)
        ]
        public MobileListItemCollection Items
        {
            get
            {
                return DataHelper.Items;
            }
        }

        /// <include file='doc\List.uex' path='docs/doc[@for="List.EnsureTemplatedUI"]/*' />
        public override void EnsureTemplatedUI()
        {
            EnsureChildControls();
        }

        /// <include file='doc\List.uex' path='docs/doc[@for="List.AddParsedSubObject"]/*' />
        protected override void AddParsedSubObject(Object obj)
        {
            if (!(obj is LiteralControl))
            {
                if (obj is MobileListItem)
                {
                    DataHelper.AddItem((MobileListItem)obj);
                } 
                else
                {
                    base.AddParsedSubObject(obj);
                }
            }
        }

        /// <include file='doc\List.uex' path='docs/doc[@for="List.CreateChildControls"]/*' />
        protected override void CreateChildControls() 
        {
            CreateChildControls(false);
        }

        /// <include file='doc\List.uex' path='docs/doc[@for="List.EnsureChildControls"]/*' />
        protected override void EnsureChildControls()
        {
            // Whenever EnsureChildControls is called before items are created 
            // (e.g., at LoadPrivateViewState), there are no controls.  
            // Rebuild children for this case by setting ChildControlsCreated to false.
            ChildControlsCreated = ChildControlsCreated && Controls.Count > 0;
            base.EnsureChildControls();
        }

        private void CreateChildControls(bool doDataBind)
        {
            if (IsTemplated && DataHelper.HasItems() && Items.Count > 0)
            {
                Controls.Clear();
                CreateTemplatedUI(doDataBind);
            }
            ChildControlsCreated = true;
        }
        
        /// <include file='doc\List.uex' path='docs/doc[@for="List.OnDataBinding"]/*' />
        protected override void OnDataBinding(EventArgs e) 
        {
            base.OnDataBinding(e);
            if (DataHelper.ResolvedDataSource != null)
            {
                Controls.Clear();
                ClearChildViewState();
                CreateItems(DataHelper.ResolvedDataSource);
                CreateChildControls(true);
            }
        }

        /// <include file='doc\List.uex' path='docs/doc[@for="List.CreateItems"]/*' />
        protected virtual void CreateItems(IEnumerable dataSource) 
        {
            DataHelper.CreateItems(dataSource);
        }

        /// <include file='doc\List.uex' path='docs/doc[@for="List.CreateDefaultTemplatedUI"]/*' />
        public override void CreateDefaultTemplatedUI(bool doDataBind) 
        {
            MobileListItemCollection items = Items;

            ITemplate headerTemplate = GetTemplate(Constants.HeaderTemplateTag);
            ITemplate footerTemplate = GetTemplate(Constants.FooterTemplateTag);
            ITemplate itemTemplate = GetTemplate(Constants.ItemTemplateTag);
            ITemplate separatorTemplate = GetTemplate(Constants.SeparatorTemplateTag);
            ITemplate alternatingItemTemplate = GetTemplate(Constants.AlternatingItemTemplateTag);
            if (alternatingItemTemplate == null)
            {
                alternatingItemTemplate = itemTemplate;
            }

            CreateControlItem(MobileListItemType.HeaderItem, 
                              headerTemplate,
                              doDataBind);
            for (int i = 0; i < items.Count; i++)
            {
                if (i > 0)
                {
                    CreateControlItem(MobileListItemType.SeparatorItem, 
                                      separatorTemplate, 
                                      doDataBind);
                }
                AddItemAsControl(i, 
                                 items[i], 
                                 ((i & 1) == 1) ? alternatingItemTemplate : itemTemplate, 
                                 doDataBind);
            }
            CreateControlItem(MobileListItemType.FooterItem, 
                              footerTemplate,
                              doDataBind);
        }

        private void AddItemAsControl(
            int itemIndex,
            MobileListItem item,
            ITemplate itemTemplate, 
            bool doDataBind)
        {
            if (itemTemplate != null)
            {
                item.Controls.Clear();
                CheckedInstantiateTemplate (itemTemplate, item, this);
                Controls.Add(item);
                if (doDataBind)
                {
                    item.DataBind();
                }
            }
        }

        private void CreateControlItem(
            MobileListItemType itemType, 
            ITemplate itemTemplate, 
            bool doDataBind)
        {
            if (itemTemplate != null)
            {
                MobileListItem item = new MobileListItem(itemType);
                AddItemAsControl(-1, item, itemTemplate, doDataBind);
            }
        }

        /// <include file='doc\List.uex' path='docs/doc[@for="List.OnPreRender"]/*' />
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (IsTemplated)
            {
                int firstVisibleItem = FirstVisibleItemIndex;
                int pageSize = VisibleItemCount;
                int lastVisibleItem = firstVisibleItem + pageSize - 1;
                int itemIndex = 0;
                int separatorIndex = 0;
                foreach(MobileListItem item in Controls)
                {
                    if (item.ItemType == MobileListItemType.ListItem)
                    {
                        item.Visible = itemIndex >= firstVisibleItem && itemIndex <= lastVisibleItem;
                        itemIndex++;
                    }
                    else if (item.ItemType == MobileListItemType.SeparatorItem)
                    {
                        item.Visible = separatorIndex >= firstVisibleItem && 
                                            separatorIndex < lastVisibleItem;
                        separatorIndex++;
                    }
                }
            }
        }

        /// <include file='doc\List.uex' path='docs/doc[@for="List.InternalItemCount"]/*' />
        protected override int InternalItemCount
        {
            get
            {
                if (DataHelper.HasItems())
                {
                    Debug.Assert (Items.Count >= 0);
                    return Items.Count;
                }
                else
                {
                    return 0;
                }
            }
            
        }

        /// <include file='doc\List.uex' path='docs/doc[@for="List.OnPageChange"]/*' />
        protected override void OnPageChange(int oldPageIndex, int newPageIndex)
        {
            base.OnPageChange(oldPageIndex, newPageIndex);
        }

        /////////////////////////////////////////////////////////////////////////
        //  EVENT HANDLING
        /////////////////////////////////////////////////////////////////////////

        /// <include file='doc\List.uex' path='docs/doc[@for="List.ItemCommand"]/*' />
        [
            MobileCategory(SR.Category_Action),
            MobileSysDescription(SR.List_OnItemCommand)
        ]
        public event ListCommandEventHandler ItemCommand 
        {
            add 
            {
                Events.AddHandler(EventItemCommand, value);
            }
            remove 
            {
                Events.RemoveHandler(EventItemCommand, value);
            }
        }

        /// <include file='doc\List.uex' path='docs/doc[@for="List.OnItemCommand"]/*' />
        protected virtual void OnItemCommand(ListCommandEventArgs e) 
        {
            ListCommandEventHandler onItemCommandHandler = (ListCommandEventHandler)Events[EventItemCommand];
            if (onItemCommandHandler != null)
            {
                onItemCommandHandler(this, e);
            }
        }


        /// <include file='doc\List.uex' path='docs/doc[@for="List.HasItemCommandHandler"]/*' />
        [
            Bindable(false),
            Browsable(false),
            DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
        ]
        public bool HasItemCommandHandler
        {
            get
            {
                return Events[EventItemCommand] != null;
            }
        }

        /// <include file='doc\List.uex' path='docs/doc[@for="List.Decoration"]/*' />
        [
            Bindable(true),
            DefaultValue(ListDecoration.None),
            MobileCategory(SR.Category_Appearance),
            MobileSysDescription(SR.List_Decoration)
        ]
        public ListDecoration Decoration
        {
            get
            {
                return _decoration;
            }
            set
            {
                _decoration = value;
            }
        }

        /// <include file='doc\List.uex' path='docs/doc[@for="List.ItemDataBind"]/*' />
        [
            MobileCategory(SR.Category_Action),
            MobileSysDescription(SR.List_OnItemDataBind)
        ]
        public event ListDataBindEventHandler ItemDataBind 
        {
            add 
            {
                Events.AddHandler(EventItemDataBind, value);
            }
            remove 
            {
                Events.RemoveHandler(EventItemDataBind, value);
            }
        }

        /// <include file='doc\List.uex' path='docs/doc[@for="List.OnItemDataBind"]/*' />
        protected virtual void OnItemDataBind(ListDataBindEventArgs e) 
        {
            ListDataBindEventHandler onItemDataBindHandler = 
                (ListDataBindEventHandler)Events[EventItemDataBind];
            if (onItemDataBindHandler != null)
            {
                onItemDataBindHandler(this, e);
            }
        }

        /// <include file='doc\List.uex' path='docs/doc[@for="List.OnBubbleEvent"]/*' />
        protected override bool OnBubbleEvent(Object sender, EventArgs e) 
        {
            bool handled = false;

            if (e is ListCommandEventArgs) 
            {
                OnItemCommand((ListCommandEventArgs)e);
                handled = true;
            }

            return handled;
        }

        /// <internalonly/>
        protected void RaisePostBackEvent(String eventArgument)
        {
            if(!ItemsAsLinks)
            {
                // Non-templated list got a click event. There really
                // isn't a command source, so we'll set it to the 
                // default of null.
                int item = Int32.Parse(eventArgument, CultureInfo.InvariantCulture);
                item = TranslateVirtualItemIndex(item);
                OnItemCommand(new ListCommandEventArgs(Items[item], null));
            }
            else
            {
                MobilePage.ActiveForm = MobilePage.GetForm(eventArgument);
            }
        }

        private int TranslateVirtualItemIndex(int itemIndex)
        {
            // Ensure that the item of the given virtual index is loaded,
            // and translate as necessary to an index within the collection.

            if (ItemCount > 0)
            {
                if (DataHelper.HasItems() && Items.Count > itemIndex - Items.BaseIndex)
                {
                    itemIndex -= Items.BaseIndex;
                }
                else
                {
                    OnLoadItems(new LoadItemsEventArgs(itemIndex, 1));
                    itemIndex = 0;
                }
            }

            return itemIndex;
        }

        /// <include file='doc\List.uex' path='docs/doc[@for="List.OnLoadItems"]/*' />
        protected override void OnLoadItems(LoadItemsEventArgs e)
        {
            // We should only load items if the base index has changed, or if
            // the desired items do not exist in the list. Otherwise, we are making
            // the app reload the same items over and over.

            if (e.ItemIndex != Items.BaseIndex || e.ItemCount != Items.Count)
            {
                Items.BaseIndex = e.ItemIndex;
                Items.Clear();
                base.OnLoadItems(e);
            }
        }

        /////////////////////////////////////////////////////////////////////////
        //  STATE MANAGEMENT
        /////////////////////////////////////////////////////////////////////////

        /// <include file='doc\List.uex' path='docs/doc[@for="List.TrackViewState"]/*' />
        protected override void TrackViewState() 
        {
            base.TrackViewState();
            if (DataHelper.HasItems())
            {
                ((IStateManager)Items).TrackViewState();
            }
        }

        /// <include file='doc\List.uex' path='docs/doc[@for="List.SaveViewState"]/*' />
        protected override Object SaveViewState() 
        {
            Object baseState, itemsState;

            if (DataHelper.HasItems())
            {
                itemsState = ((IStateManager)Items).SaveViewState();
            }
            else
            {
                itemsState = null;
            }
            baseState = base.SaveViewState();

            if (itemsState != null || Decoration != ListDecoration.None )
            {
                return new Object[3] { baseState, itemsState, Decoration };
            }
            else if (baseState != null)
            {
                return new Object[1] { baseState };
            }
            return null;
        }

        /// <include file='doc\List.uex' path='docs/doc[@for="List.LoadViewState"]/*' />
        protected override void LoadViewState(Object savedState) 
        {
            if (savedState != null) 
            {
                Object[] state = (Object[])savedState;
                base.LoadViewState(state[0]);

                if (state.Length > 1)
                {
                    ((IStateManager)Items).LoadViewState(state[1]);
                    Decoration = (ListDecoration) state[2];
                }
            }
        }

        internal override void InternalItemCountChangedHandler(int newItemCount)
        {
            if (newItemCount == 0)
            {
                Items.Clear();
                Controls.Clear();
            }
        }

        /////////////////////////////////////////////////////////////////////////
        //  IListControl
        /////////////////////////////////////////////////////////////////////////

        /// <include file='doc\List.uex' path='docs/doc[@for="List.IListControl.OnItemDataBind"]/*' />
        /// <internalonly/>
        void IListControl.OnItemDataBind(ListDataBindEventArgs e) 
        {
            OnItemDataBind(e);
        }

        /// <include file='doc\List.uex' path='docs/doc[@for="List.IListControl.TrackingViewState"]/*' />
        /// <internalonly/>
        bool IListControl.TrackingViewState
        {
            get
            {
                return IsTrackingViewState;
            }
        }

        #region IPostBackEventHandler implementation
        void IPostBackEventHandler.RaisePostBackEvent(String eventArgument) {
            RaisePostBackEvent(eventArgument);
        }
        #endregion 
    }
}