Merge pull request #2916 from ludovic-henry/fix-40306
[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-2010 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 using System.Collections;
34 using System.Collections.Specialized;
35 using System.Text;
36 using System.Web.Util;
37 using System.ComponentModel;
38 using System.Security.Permissions;
39 using System.Web.UI.WebControls.Adapters;
40
41 namespace System.Web.UI.WebControls
42 {
43         // CAS
44         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
45         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
46         // attributes
47         [DesignerAttribute ("System.Web.UI.Design.WebControls.DataBoundControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
48         public abstract class DataBoundControl : BaseDataBoundControl
49         {
50                 DataSourceSelectArguments selectArguments;
51                 DataSourceView currentView;
52
53                 protected DataBoundControl ()
54                 {
55                 }
56
57                 /* Used for controls that used to inherit from
58                  * WebControl, so the tag can propagate upwards
59                  */
60                 internal DataBoundControl (HtmlTextWriterTag tag) : base (tag)
61                 {
62                 }
63                 
64                 protected virtual IDataSource GetDataSource ()
65                 {
66                         if (IsBoundUsingDataSourceID) {
67                                 Control ctrl = FindDataSource ();
68
69                                 if (ctrl == null)
70                                         throw new HttpException (string.Format ("A control with ID '{0}' could not be found.", DataSourceID));
71                                 if (!(ctrl is IDataSource))
72                                         throw new HttpException (string.Format ("The control with ID '{0}' is not a control of type IDataSource.", DataSourceID));
73                                 return (IDataSource) ctrl;
74                         }
75                         
76                         IDataSource ds = DataSource as IDataSource;
77                         if (ds != null)
78                                 return ds;
79                         
80                         IEnumerable ie = DataSourceResolver.ResolveDataSource (DataSource, DataMember);
81                         return new CollectionDataSource (ie);
82                 }
83                 
84                 protected virtual DataSourceView GetData ()
85                 {
86                         if (currentView == null)
87                                 UpdateViewData ();
88                         return currentView;
89                 }
90                 
91                 DataSourceView InternalGetData ()
92                 {
93                         if (currentView != null)
94                                 return currentView;
95                         
96                         if (DataSource != null && IsBoundUsingDataSourceID)
97                                 throw new HttpException ("Control bound using both DataSourceID and DataSource properties.");
98                         
99                         IDataSource ds = GetDataSource ();
100                         if (ds != null)
101                                 return ds.GetView (DataMember);
102                         else
103                                 return null; 
104                 }
105                 
106                 protected override void OnDataPropertyChanged ()
107                 {
108                         base.OnDataPropertyChanged ();
109                         currentView = null;
110                 }
111                 
112                 protected virtual void OnDataSourceViewChanged (object sender, EventArgs e)
113                 {
114                         RequiresDataBinding = true;
115                 }
116                 
117                 // MSDN: The OnPagePreLoad method is overridden by the DataBoundControl class 
118                 // to set the BaseDataBoundControl.RequiresDataBinding property to true in 
119                 // cases where the HTTP request is a postback and view state is enabled but 
120                 // the data-bound control has not yet been bound.
121                 //
122                 // LAMESPEC: RequiresDataBinding is also set when http request is NOT a postback -
123                 // no matter whether view state is enabled. The correct description should be:
124                 //
125                 // The OnPagePreLoad method is override by the DataBoundControl class 
126                 // to set the BaseDataBoundControl.RequiresDataBinding property to true in 
127                 // cases where the HTTP request is not a postback or it is a postback and view state
128                 // is enabled but the data-bound control has not yet been bound.
129                 protected override void OnPagePreLoad (object sender, EventArgs e)
130                 {
131                         base.OnPagePreLoad (sender, e);
132
133                         Initialize ();
134                 }
135
136                 void Initialize ()
137                 {
138                         Page page = Page;
139                         if (page != null && !IsDataBound) {
140                                 // LAMESPEC: see the comment above OnPagePreLoad
141                                 if (!page.IsPostBack)
142                                         RequiresDataBinding = true;
143                                 else if (IsViewStateEnabled)
144                                         RequiresDataBinding = true;
145                         }
146                         
147                 }
148                 
149                 void UpdateViewData ()
150                 {
151                         if (currentView != null)
152                                 currentView.DataSourceViewChanged -= new EventHandler (OnDataSourceViewChanged);
153                         
154                         DataSourceView view = InternalGetData ();
155                         if (view != currentView)
156                                 currentView = view;
157
158                         if (currentView != null)
159                                 currentView.DataSourceViewChanged += new EventHandler (OnDataSourceViewChanged);
160                 }
161                 
162                 protected internal override void OnLoad (EventArgs e)
163                 {
164                         UpdateViewData ();
165                         if (!Initialized) {
166                                 Initialize ();
167
168                                 // MSDN: The ConfirmInitState method sets the initialized state of the data-bound 
169                                 // control. The method is called by the DataBoundControl class in its OnLoad
170                                 // method.
171                                 ConfirmInitState ();
172                         }
173                         
174                         base.OnLoad(e);
175                 }
176                 
177                 protected internal virtual void PerformDataBinding (IEnumerable data)
178                 {
179                 }
180
181                 protected override void ValidateDataSource (object dataSource)
182                 {
183                         if (dataSource == null || dataSource is IListSource || dataSource is IEnumerable || dataSource is IDataSource)
184                                 return;
185                         throw new ArgumentException ("Invalid data source source type. The data source must be of type IListSource, IEnumerable or IDataSource.");
186                 }
187
188                 [ThemeableAttribute (false)]
189                 [DefaultValueAttribute ("")]
190                 [WebCategoryAttribute ("Data")]
191                 public virtual string DataMember {
192                         get { return ViewState.GetString ("DataMember", String.Empty); }
193                         set { ViewState["DataMember"] = value; }
194                 }
195
196                 [IDReferencePropertyAttribute (typeof(DataSourceControl))]
197                 public override string DataSourceID {
198                         get { return ViewState.GetString ("DataSourceID", String.Empty); }
199                         set {
200                                 ViewState ["DataSourceID"] = value;
201                                 base.DataSourceID = value;
202                         }
203                 }
204
205                 [Browsable (false)]
206                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
207                 public IDataSource DataSourceObject {
208                         get { return GetDataSource (); }
209                 }
210                 
211                 // 
212                 // See DataBoundControl.MarkAsDataBound msdn doc for the code example
213                 // 
214                 protected override void PerformSelect ()
215                 {
216                         // Call OnDataBinding here if bound to a data source using the
217                         // DataSource property (instead of a DataSourceID), because the
218                         // databinding statement is evaluated before the call to GetData.       
219                         if (!IsBoundUsingDataSourceID)
220                                 OnDataBinding (EventArgs.Empty);
221
222                         // prevent recursive calls
223                         RequiresDataBinding = false;
224                         SelectArguments = CreateDataSourceSelectArguments ();
225                         GetData ().Select (SelectArguments, new DataSourceViewSelectCallback (OnSelect));
226
227                         // The PerformDataBinding method has completed.
228                         MarkAsDataBound ();
229                         
230                         // Raise the DataBound event.
231                         OnDataBound (EventArgs.Empty);
232                 }
233                 
234                 void OnSelect (IEnumerable data)
235                 {
236                         // Call OnDataBinding only if it has not already been 
237                         // called in the PerformSelect method.
238                         if (IsBoundUsingDataSourceID)
239                                 OnDataBinding (EventArgs.Empty);
240                         // The PerformDataBinding method binds the data in the  
241                         // retrievedData collection to elements of the data-bound control.
242                         InternalPerformDataBinding (data);
243                 }
244
245                 internal void InternalPerformDataBinding (IEnumerable data)
246                 {
247                         DataBoundControlAdapter adapter = Adapter as DataBoundControlAdapter;
248                         if (adapter != null)
249                                 adapter.PerformDataBinding (data);
250                         else
251                                 PerformDataBinding (data);
252                 }
253                 
254                 protected virtual DataSourceSelectArguments CreateDataSourceSelectArguments ()
255                 {
256                         return DataSourceSelectArguments.Empty;
257                 }
258                 
259                 protected DataSourceSelectArguments SelectArguments {
260                         get {
261                                 if (selectArguments == null)
262                                         selectArguments = CreateDataSourceSelectArguments ();
263                                 return selectArguments;
264                         }
265                         private set {
266                                 selectArguments = value;
267                         }
268                 }
269
270                 bool IsDataBound {
271                         get {
272                                 object dataBound = ViewState ["DataBound"];
273                                 return dataBound != null ? (bool) dataBound : false;
274                         }
275                         set {
276                                 ViewState ["DataBound"] = value;
277                         }
278                 }
279
280                 protected void MarkAsDataBound ()
281                 {
282                         IsDataBound = true;
283                 }
284         }
285 }
286
287
288
289
290
291