[bcl] Rename variables to avoid conflict with later renames
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / HierarchicalDataBoundControl.cs
1 //
2 // System.Web.UI.WebControls.HierarchicalDataBoundControl.cs
3 //
4 // Authors:
5 //      Lluis Sanchez Gual (lluis@novell.com)
6 //
7 // (C) 2004-2010 Novell, Inc (http://www.novell.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Collections;
32 using System.ComponentModel;
33 using System.Web.UI;
34 using System.Web.UI.WebControls.Adapters;
35
36 namespace System.Web.UI.WebControls
37 {
38         [DesignerAttribute ("System.Web.UI.Design.WebControls.HierarchicalDataBoundControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
39         public abstract class HierarchicalDataBoundControl : BaseDataBoundControl
40         {
41                 [IDReferencePropertyAttribute (typeof(HierarchicalDataSourceControl))]
42                 public override string DataSourceID {
43                         get {
44                                 object o = ViewState ["DataSourceID"];
45                                 if (o != null)
46                                         return (string)o;
47                                 
48                                 return String.Empty;
49                         }
50                         set {
51                                 if (Initialized)
52                                         RequiresDataBinding = true;
53                                 
54                                 ViewState ["DataSourceID"] = value;
55                         }
56                 }
57                 
58                 protected virtual HierarchicalDataSourceView GetData (string viewPath)
59                 {
60                         if (DataSource != null && !String.IsNullOrEmpty (DataSourceID))
61                                 throw new HttpException ();     
62                         IHierarchicalDataSource ds = GetDataSource ();
63                         if (ds != null)
64                                 return ds.GetHierarchicalView (viewPath);
65                         
66                         if (DataSource is IHierarchicalEnumerable)
67                                 return new ReadOnlyDataSourceView ((IHierarchicalEnumerable) DataSource);
68                         
69                         return null;
70                 }
71                 
72                 protected virtual IHierarchicalDataSource GetDataSource ()
73                 {
74                         if (IsBoundUsingDataSourceID) {
75                                 Control ctrl = FindDataSource ();
76
77                                 if (ctrl == null)
78                                         throw new HttpException (string.Format ("A control with ID '{0}' could not be found.", DataSourceID));
79                                 if (!(ctrl is IHierarchicalDataSource))
80                                         throw new HttpException (string.Format ("The control with ID '{0}' is not a control of type IHierarchicalDataSource.", DataSourceID));
81                                 return (IHierarchicalDataSource) ctrl;
82                         }
83                         
84                         return DataSource as IHierarchicalDataSource;
85                 }
86
87                 bool IsDataBound {
88                         get { return ViewState.GetBool ("DataBound", false); }
89                         set { ViewState ["DataBound"] = value; }
90                 }
91
92                 protected void MarkAsDataBound ()
93                 {
94                         IsDataBound = true;
95                 }
96                 
97                 protected override void OnDataPropertyChanged ()
98                 {
99                         RequiresDataBinding = true;
100                 }
101                 
102                 protected virtual void OnDataSourceChanged (object sender, EventArgs e)
103                 {
104                         RequiresDataBinding = true;
105                 }
106
107                 protected internal override void OnLoad (EventArgs e)
108                 {
109                         if (!Initialized) {
110                                 Initialize ();
111                                 ConfirmInitState ();
112                         }
113                         
114                         base.OnLoad(e);
115                 }
116
117                 void Initialize ()
118                 {
119                         if (!Page.IsPostBack || (IsViewStateEnabled && !IsDataBound))
120                                 RequiresDataBinding = true;
121
122                         IHierarchicalDataSource ds = GetDataSource ();
123                         if (ds != null && DataSourceID != "")
124                                 ds.DataSourceChanged += new EventHandler (OnDataSourceChanged);
125                 }
126
127                 protected override void OnPagePreLoad (object sender, EventArgs e)
128                 {
129                         base.OnPagePreLoad (sender, e);
130                         
131                         Initialize ();
132                 }
133                 
134                 protected void InternalPerformDataBinding ()
135                 {
136                         HierarchicalDataBoundControlAdapter adapter 
137                                 = Adapter as HierarchicalDataBoundControlAdapter;
138                         if (adapter != null)
139                                 adapter.PerformDataBinding ();
140                         else
141                                 PerformDataBinding ();
142                 }
143                 
144                 protected internal virtual void PerformDataBinding ()
145                 {
146                 }
147                 
148                 protected override void PerformSelect ()
149                 {
150                         OnDataBinding (EventArgs.Empty);
151                         InternalPerformDataBinding ();
152                         // The PerformDataBinding method has completed.
153                         RequiresDataBinding = false;
154                         MarkAsDataBound ();
155                         OnDataBound (EventArgs.Empty);
156                 }
157                 
158                 protected override void ValidateDataSource (object dataSource)
159                 {
160                         if (dataSource == null || dataSource is IHierarchicalDataSource || dataSource is IHierarchicalEnumerable)
161                                 return;
162                         throw new InvalidOperationException ("Invalid data source");
163                 }
164         }
165 }
166
167
168