**** Merged r40732-r40872 from MCS ****
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / BaseDataBoundControl.cs
1 //
2 // System.Web.UI.WebControls.BaseDataBoundControl.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
35 namespace System.Web.UI.WebControls
36 {
37         [DefaultProperty ("DataSourceID")]
38         [DesignerAttribute ("System.Web.UI.Design.WebControls.BaseDataBoundControlDesigner, System.Design, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.IDesigner")]
39         public abstract class BaseDataBoundControl: WebControl
40         {
41                 public event EventHandler DataBound;
42                 
43                 object dataSource;
44                 string dataSourceId;
45                 bool initialized;
46                 bool requiresDataBinding;
47                 
48                 [BindableAttribute (true)]
49                 [ThemeableAttribute (false)]
50                 [DefaultValueAttribute (null)]
51                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
52                 public virtual object DataSource {
53                         get {
54                                 return dataSource;
55                         }
56                         set {
57                                 ValidateDataSource (value);
58                                 dataSource = value;
59                                 if (initialized)
60                                         OnDataPropertyChanged ();
61                         }
62                 }
63                 
64                 [DefaultValueAttribute ("")]
65                 [ThemeableAttribute (false)]
66                 public virtual string DataSourceID {
67                         get {
68                                 return dataSourceId != null ? dataSourceId : string.Empty;
69                         }
70                         set {
71                                 dataSourceId = value;
72                                 if (initialized)
73                                         OnDataPropertyChanged ();
74                         }
75                 }
76                 
77                 protected bool Initialized {
78                         get { return initialized; }
79                 }
80                 
81                 protected bool IsBoundUsingDataSourceID {
82                         get { return DataSourceID.Length > 0; }
83                 }
84                 
85                 protected bool RequiresDataBinding {
86                         get { return requiresDataBinding; }
87                         set { requiresDataBinding = value; }
88                 }
89                 
90                 protected void ConfirmInitState ()
91                 {
92                         initialized = true;
93                 }
94                 
95                 public override void DataBind ()
96                 {
97                         RequiresDataBinding = false;
98                         PerformSelect ();
99                         OnDataBound (EventArgs.Empty);
100                 }
101                 
102                 protected void EnsureDataBound ()
103                 {
104                         if (RequiresDataBinding && IsBoundUsingDataSourceID)
105                                 DataBind ();
106                 }
107                 
108                 protected virtual void OnDataBound (EventArgs e)
109                 {
110                         if (DataBound != null)
111                                 DataBound (this, e);
112                 }
113
114                 protected virtual void OnDataPropertyChanged ()
115                 {
116                         RequiresDataBinding = true;
117                 }
118                 
119                 protected override void OnInit (EventArgs e)
120                 {
121                         base.OnInit (e);
122                         Page.PreLoad += new EventHandler (OnPagePreLoad);
123                 }
124                 
125                 protected virtual void OnPagePreLoad (object sender, EventArgs e)
126                 {
127                         ConfirmInitState ();
128                 }
129                 
130                 protected override void OnPreRender (EventArgs e)
131                 {
132                         EnsureDataBound ();
133                         base.OnPreRender (e);
134                 }
135                 
136                 protected abstract void PerformSelect ();
137                 
138                 protected abstract void ValidateDataSource (object dataSource);
139                 
140         }
141 }
142
143 #endif
144