2004-04-22 Martin Baulig <martin@ximian.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 //
7 // (C) 2003 Ben Maurer
8 //
9
10 #if NET_2_0
11 using System.Collections;
12 using System.Collections.Specialized;
13 using System.Text;
14 using System.Web.Util;
15 using System.ComponentModel;
16
17 namespace System.Web.UI.WebControls {
18         public abstract class DataBoundControl : WebControl
19         {
20                 public event EventHandler DataBound;
21                 
22                 protected DataBoundControl ()
23                 {
24                 }
25                 
26                 public sealed override void DataBind ()
27                 {
28                         PerformDataBinding ();
29                         RequiresDataBinding = false;
30                         OnDataBound (EventArgs.Empty);
31                 }
32                 
33                 protected void EnsureDataBound ()
34                 {
35                         if (RequiresDataBinding && DataSourceID != "")
36                                 DataBind ();
37                 }
38
39                 protected virtual object GetDataSourceObject()
40                 {
41                         if (DataSourceID != "")
42                                 return (IDataSource) NamingContainer.FindControl (DataSourceID);
43                         
44                         return DataSource;
45                 }
46                 
47                 
48                 protected virtual IEnumerable GetResolvedDataSource ()
49                 {
50                         if (DataSource != null && DataSourceID != "")
51                                 throw new HttpException ();
52                         
53                         IDataSource ds = GetDataSourceObject () as IDataSource;
54                         if (ds != null && DataSourceID != "")
55                                 return ds.GetView (DataMember).Select ();
56                         else if (DataSource != null)
57                                 return DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
58                         else
59                                 return null; 
60                 }
61                 
62                 protected bool IsBoundToDataSourceControl()
63                 {
64                         return (GetDataSourceObject () is IDataSource) && DataSourceID != "";
65                 }
66                 
67                 protected virtual void OnDataBound(EventArgs e) {
68                         if (DataBound != null)
69                                 DataBound (this, e);
70                 }
71                 
72                 protected virtual void OnDataPropertyChanged ()
73                 {
74                         this.RequiresDataBinding = true;
75                 }
76                 
77                 protected virtual void OnDataSourceChanged (object sender, EventArgs e)
78                 {
79                         RequiresDataBinding = true;
80                 }
81                 
82                 // should be `internal protected' (why, oh WHY did they do that !?!)
83                 protected override void OnInit (EventArgs e)
84                 {
85                         base.OnInit(e);
86                         inited = true;
87                         if (!Page.IsPostBack)
88                                 RequiresDataBinding = true;
89                 }
90                 
91                 // should be `internal protected' (why, oh WHY did they do that !?!)
92                 protected override void OnLoad (EventArgs e)
93                 {
94                         IDataSource ds = GetDataSourceObject () as IDataSource;
95                         if (ds != null && DataSourceID != "")
96                                 ds.DataSourceChanged += new EventHandler (OnDataSourceChanged);
97                         
98                         base.OnLoad(e);
99                 }
100                 
101                 // should be `internal protected' (why, oh WHY did they do that !?!)
102                 protected override void OnPreRender (EventArgs e)
103                 {
104                         EnsureDataBound ();
105                         base.OnPreRender (e);
106                 }
107                 
108                 protected virtual void PerformDataBinding ()
109                 {
110                         OnDataBinding(EventArgs.Empty);
111                 }
112
113                 
114                 protected virtual void ValidateDataSource (object dataSource)
115                 {
116                         if (dataSource is IListSource || dataSource is IEnumerable)
117                                 return;
118                         throw new ArgumentException ();
119                 }
120
121
122                 public string DataMember
123                 {
124                         get {
125                                 object o = ViewState["DataMember"];
126                                 if(o!=null)
127                                         return (string)o;
128                                 return String.Empty;
129                         }
130                         set {
131                                 ViewState["DataMember"] = value;
132                         }
133                 }
134
135                 object dataSource;
136                 public virtual object DataSource
137                 {
138                         get {
139                                 return dataSource;
140                         }
141                         set {
142                                 ValidateDataSource (value);
143                                 dataSource = value;
144                         }
145                 }
146                 
147                 public virtual string DataSourceID {
148                         get {
149                                 object o = ViewState ["DataSourceID"];
150                                 if (o != null)
151                                         return (string)o;
152                                 
153                                 return String.Empty;
154                         }
155                         set {
156                                 if (inited)
157                                         RequiresDataBinding = true;
158                                 
159                                 ViewState ["DataSourceID"] = value;
160                         }
161                 }
162                 
163                 bool requiresDataBinding;
164                 protected bool RequiresDataBinding {
165                         get { return requiresDataBinding; }
166                         set { requiresDataBinding = value; }
167                 }
168                 
169                 protected bool inited;
170         }
171 }
172 #endif
173