2 // System.Web.UI.WebControls.BaseDataBoundControl.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
7 // Copyright (C) 2004-2010 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Collections;
30 using System.ComponentModel;
31 using System.Security.Permissions;
33 namespace System.Web.UI.WebControls
36 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39 [DefaultProperty ("DataSourceID")]
40 [DesignerAttribute ("System.Web.UI.Design.WebControls.BaseDataBoundControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
41 public abstract class BaseDataBoundControl: WebControl
43 static readonly object dataBoundEvent = new object ();
45 EventHandlerList events = new EventHandlerList ();
49 bool requiresDataBinding;
51 public event EventHandler DataBound {
52 add { events.AddHandler (dataBoundEvent, value); }
53 remove { events.RemoveHandler (dataBoundEvent, value); }
56 protected BaseDataBoundControl ()
60 /* Used for controls that used to inherit from
61 * WebControl, so the tag can propagate upwards
63 internal BaseDataBoundControl (HtmlTextWriterTag tag) : base (tag)
67 [BindableAttribute (true)]
68 [ThemeableAttribute (false)]
69 [DefaultValueAttribute (null)]
70 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
71 public virtual object DataSource {
72 get { return dataSource; }
75 ValidateDataSource (value);
77 OnDataPropertyChanged ();
81 [DefaultValueAttribute ("")]
82 [ThemeableAttribute (false)]
83 public virtual string DataSourceID {
84 get { return ViewState.GetString ("DataSourceID", String.Empty); }
86 ViewState["DataSourceID"] = value;
87 OnDataPropertyChanged ();
91 protected bool Initialized {
92 get { return initialized; }
95 protected bool IsBoundUsingDataSourceID {
96 get { return DataSourceID.Length > 0; }
99 protected bool RequiresDataBinding {
100 get { return requiresDataBinding; }
102 // MSDN: If you set the RequiresDataBinding
103 // property to true when the data-bound control
104 // has already begun to render its output to the
105 // page, the current HTTP request is not a
106 // callback, and you are using the DataSourceID
107 // property to identify the data source control
108 // to bind to, the DataBind method is called
109 // immediately. In this case, the
110 // RequiresDataBinding property is _not_ actually
113 // LAMESPEC, the docs quoted above mention that
114 // DataBind is called in the described
115 // case. This is wrong since that way we don't
116 // break recursion when the property is set from
117 // within the OnSelect handler in user's
118 // code. EnsureDataBound makes sure that no
119 // recursive binding is performed. Also the
120 // property DOES get set in this case (according
122 if (value && preRendered && IsBoundUsingDataSourceID && Page != null && !Page.IsCallback) {
123 requiresDataBinding = true;
126 requiresDataBinding = value;
130 public override bool SupportsDisabledAttribute {
131 get { return RenderingCompatibilityLessThan40; }
134 protected void ConfirmInitState ()
139 public override void DataBind ()
144 protected virtual void EnsureDataBound ()
146 if (RequiresDataBinding && IsBoundUsingDataSourceID)
150 protected virtual void OnDataBound (EventArgs e)
152 EventHandler eh = events [dataBoundEvent] as EventHandler;
157 protected virtual void OnDataPropertyChanged ()
160 RequiresDataBinding = true;
163 protected internal override void OnInit (EventArgs e)
166 Page.PreLoad += new EventHandler (OnPagePreLoad);
168 if (!IsViewStateEnabled && Page != null && Page.IsPostBack)
169 RequiresDataBinding = true;
172 protected virtual void OnPagePreLoad (object sender, EventArgs e)
177 protected internal override void OnPreRender (EventArgs e)
181 base.OnPreRender (e);
184 internal Control FindDataSource ()
187 Control namingContainer = NamingContainer;
188 string dataSourceID = DataSourceID;
190 while (namingContainer != null) {
191 ctrl = namingContainer.FindControl (dataSourceID);
194 namingContainer = namingContainer.NamingContainer;
200 protected abstract void PerformSelect ();
202 protected abstract void ValidateDataSource (object dataSource);