++ Test/mainsoft/ChangeLog (working copy)
[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 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 #if NET_2_0
32 using System.Collections;
33 using System.ComponentModel;
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 && DataSourceID != "")
61                                 throw new HttpException ();
62                         
63                         IHierarchicalDataSource ds = GetDataSource ();
64                         if (ds != null)
65                                 return ds.GetHierarchicalView (viewPath);
66                         else
67                                 return null; 
68                 }
69                 
70                 protected virtual IHierarchicalDataSource GetDataSource ()
71                 {
72                         if (IsBoundUsingDataSourceID) {
73                                 
74                                 Control ctrl = FindDataSource ();
75
76                                 if (ctrl == null)
77                                         throw new HttpException (string.Format ("A control with ID '{0}' could not be found.", DataSourceID));
78                                 if (!(ctrl is IHierarchicalDataSource))
79                                         throw new HttpException (string.Format ("The control with ID '{0}' is not a control of type IHierarchicalDataSource.", DataSourceID));
80                                 return (IHierarchicalDataSource) ctrl;
81                         }
82                         
83                         return DataSource as IHierarchicalDataSource;
84                 }
85
86                 bool IsDataBound {
87                         get {
88                                 return ViewState.GetBool ("DataBound", false);
89                         }
90                         set {
91                                 ViewState ["DataBound"] = value;
92                         }
93                 }
94
95                 protected void MarkAsDataBound ()
96                 {
97                         IsDataBound = true;
98                 }
99                 
100                 protected override void OnDataPropertyChanged ()
101                 {
102                         RequiresDataBinding = true;
103                 }
104                 
105                 protected virtual void OnDataSourceChanged (object sender, EventArgs e)
106                 {
107                         RequiresDataBinding = true;
108                 }
109
110                 protected internal override void OnLoad (EventArgs e)
111                 {
112                         if (!Initialized) {
113                                 Initialize ();
114                                 ConfirmInitState ();
115                         }
116                         
117                         base.OnLoad(e);
118                 }
119
120                 private void Initialize ()
121                 {
122                         if (!Page.IsPostBack || (IsViewStateEnabled && !IsDataBound))
123                                 RequiresDataBinding = true;
124
125                         IHierarchicalDataSource ds = GetDataSource ();
126                         if (ds != null && DataSourceID != "")
127                                 ds.DataSourceChanged += new EventHandler (OnDataSourceChanged);
128                 }
129
130                 protected override void OnPagePreLoad (object sender, EventArgs e)
131                 {
132                         base.OnPagePreLoad (sender, e);
133                         
134                         Initialize ();
135                 }
136                 
137                 protected void InternalPerformDataBinding ()
138                 {
139                         HierarchicalDataBoundControlAdapter adapter 
140                                 = (HierarchicalDataBoundControlAdapter)Adapter;
141                         if (adapter != null)
142                                 adapter.PerformDataBinding ();
143                         else
144                                 PerformDataBinding ();
145                 }
146                 
147                 protected internal virtual void PerformDataBinding ()
148                 {
149                 }
150                 
151                 protected override void PerformSelect ()
152                 {
153                         OnDataBinding (EventArgs.Empty);
154                         InternalPerformDataBinding ();
155                         // The PerformDataBinding method has completed.
156                         RequiresDataBinding = false;
157                         MarkAsDataBound ();
158                         OnDataBound (EventArgs.Empty);
159                 }
160                 
161                 protected override void ValidateDataSource (object dataSource)
162                 {
163                         if (dataSource == null || dataSource is IHierarchicalDataSource || dataSource is IHierarchicalEnumerable)
164                                 return;
165                         throw new InvalidOperationException ("Invalid data source");
166                 }
167         }
168 }
169 #endif
170
171