2005-06-05 Peter Bartok <pbartok@novell.com>
[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                         base.DataBind ();
100                         OnDataBound (EventArgs.Empty);
101                 }
102                 
103                 protected virtual void EnsureDataBound ()
104                 {
105                         if (RequiresDataBinding && IsBoundUsingDataSourceID)
106                                 DataBind ();
107                 }
108                 
109                 protected virtual void OnDataBound (EventArgs e)
110                 {
111                         if (DataBound != null)
112                                 DataBound (this, e);
113                 }
114
115                 protected virtual void OnDataPropertyChanged ()
116                 {
117                         RequiresDataBinding = true;
118                 }
119                 
120                 protected override void OnInit (EventArgs e)
121                 {
122                         base.OnInit (e);
123                         Page.PreLoad += new EventHandler (OnPagePreLoad);
124                 }
125                 
126                 protected virtual void OnPagePreLoad (object sender, EventArgs e)
127                 {
128                         ConfirmInitState ();
129                 }
130                 
131                 protected override void OnPreRender (EventArgs e)
132                 {
133                         EnsureDataBound ();
134                         base.OnPreRender (e);
135                 }
136                 
137                 protected abstract void PerformSelect ();
138                 
139                 protected abstract void ValidateDataSource (object dataSource);
140                 
141         }
142 }
143
144 #endif
145