2005-06-05 Peter Bartok <pbartok@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / DataBoundControl.cs
1 //
2 // System.Web.UI.WebControls.DataBoundControl
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //      Sanjay Gupta (gsanjay@novell.com)
7 //
8 // (C) 2003 Ben Maurer
9 // (C) 2004 Novell, Inc. (http://www.novell.com)
10 //
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 #if NET_2_0
34 using System.Collections;
35 using System.Collections.Specialized;
36 using System.Text;
37 using System.Web.Util;
38 using System.ComponentModel;
39
40 namespace System.Web.UI.WebControls {
41
42         [DesignerAttribute ("System.Web.UI.Design.WebControls.HierarchicalDataBoundControlDesigner, System.Design, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.IDesigner")]
43         public abstract class DataBoundControl : BaseDataBoundControl
44         {
45                 DataSourceSelectArguments selectArguments;
46                 DataSourceView currentView;
47
48                 protected DataBoundControl ()
49                 {
50                 }
51                 
52                 protected IDataSource GetDataSource ()
53                 {
54                         if (IsBoundUsingDataSourceID) {
55                                 Control ctrl = NamingContainer.FindControl (DataSourceID);
56                                 if (ctrl == null)
57                                         throw new HttpException (string.Format ("A control with ID '{0}' could not be found.", DataSourceID));
58                                 if (!(ctrl is IDataSource))
59                                         throw new HttpException (string.Format ("The control with ID '{0}' is not a control of type IDataSource.", DataSourceID));
60                                 return (IDataSource) ctrl;
61                         }
62                         
63                         if (DataSource == null) return null;
64                         
65                         IDataSource ds = DataSource as IDataSource;
66                         if (ds != null) return ds;
67                         
68                         IEnumerable ie = DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
69                         if (ie != null) return new CollectionDataSource (ie);
70                         
71                         throw new HttpException (string.Format ("Unexpected data source type: {0}", DataSource.GetType()));
72                 }
73                 
74                 protected DataSourceView GetData ()
75                 {
76                         if (currentView == null)
77                                 UpdateViewData ();
78                         return currentView;
79                 }
80                 
81                 DataSourceView InternalGetData ()
82                 {
83                         if (DataSource != null && IsBoundUsingDataSourceID)
84                                 throw new HttpException ("Control bound using both DataSourceID and DataSource properties.");
85                         
86                         IDataSource ds = GetDataSource ();
87                         if (ds != null)
88                                 return ds.GetView (DataMember);
89                         else
90                                 return null; 
91                 }
92                 
93                 protected override void OnDataPropertyChanged ()
94                 {
95                         base.OnDataPropertyChanged ();
96                         UpdateViewData ();
97                 }
98                 
99                 protected virtual void OnDataSourceViewChanged (object sender, EventArgs e)
100                 {
101                         RequiresDataBinding = true;
102                 }
103                 
104                 protected override void OnPagePreLoad (object sender, EventArgs e)
105                 {
106                         base.OnPagePreLoad (sender, e);
107                         UpdateViewData ();
108                 }
109                 
110                 void UpdateViewData ()
111                 {
112                         DataSourceView view = InternalGetData ();
113                         if (view == currentView) return;
114
115                         if (currentView != null)
116                                 currentView.DataSourceViewChanged -= new EventHandler (OnDataSourceViewChanged);
117
118                         currentView = view;
119
120                         if (view != null)
121                                 view.DataSourceViewChanged += new EventHandler (OnDataSourceViewChanged);
122                 }
123                 
124                 // should be `internal protected' (why, oh WHY did they do that !?!)
125                 protected override void OnLoad (EventArgs e)
126                 {
127                         if (IsBoundUsingDataSourceID && (!Page.IsPostBack || !EnableViewState))
128                                 RequiresDataBinding = true;
129
130                         base.OnLoad(e);
131                 }
132                 
133                 protected virtual void PerformDataBinding (IEnumerable data)
134                 {
135                         OnDataBinding (EventArgs.Empty);
136                 }
137
138                 protected override void ValidateDataSource (object dataSource)
139                 {
140                         if (dataSource is IListSource || dataSource is IEnumerable || dataSource is IDataSource)
141                                 return;
142                         throw new ArgumentException ("Invalid data source source type. The data source must be of type IListSource, IEnumerable or IDataSource.");
143                 }
144
145                 [ThemeableAttribute (false)]
146                 [DefaultValueAttribute ("")]
147                 [WebCategoryAttribute ("Data")]
148                 public string DataMember
149                 {
150                         get {
151                                 object o = ViewState["DataMember"];
152                                 if(o!=null)
153                                         return (string)o;
154                                 return String.Empty;
155                         }
156                         set {
157                                 ViewState["DataMember"] = value;
158                         }
159                 }
160
161             [IDReferencePropertyAttribute (typeof(HierarchicalDataSourceControl))]
162                 public override string DataSourceID {
163                         get {
164                                 object o = ViewState ["DataSourceID"];
165                                 if (o != null)
166                                         return (string)o;
167                                 
168                                 return String.Empty;
169                         }
170                         set {
171                                 ViewState ["DataSourceID"] = value;
172                                 base.DataSourceID = value;
173                         }
174                 }
175                 
176                 protected override void PerformSelect ()
177                 {
178                         DataSourceView view = GetData ();
179                         if (view != null)
180                                 view.Select (SelectArguments, new DataSourceViewSelectCallback (OnSelect));
181                 }
182                 
183                 void OnSelect (IEnumerable data)
184                 {
185                         PerformDataBinding (data);
186                 }
187                 
188                 protected virtual DataSourceSelectArguments CreateDataSourceSelectArguments ()
189                 {
190                         return DataSourceSelectArguments.Empty;
191                 }
192                 
193                 protected DataSourceSelectArguments SelectArguments {
194                         get {
195                                 if (selectArguments == null)
196                                         selectArguments = CreateDataSourceSelectArguments ();
197                                 return selectArguments;
198                         }
199                 }
200         }
201 }
202 #endif
203