Page.Validate() is called when CausesValidation=true
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / DataBoundControl.cs
index a84d2cca7b4c2787578f5d47747ee7963de5fddb..9c64bd432f7cefd8abf950c98860e818eacecfc2 100644 (file)
@@ -36,9 +36,14 @@ using System.Collections.Specialized;
 using System.Text;
 using System.Web.Util;
 using System.ComponentModel;
+using System.Security.Permissions;
 
 namespace System.Web.UI.WebControls {
 
+       // CAS
+       [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
+       [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
+       // attributes
        [DesignerAttribute ("System.Web.UI.Design.WebControls.DataBoundControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
        public abstract class DataBoundControl : BaseDataBoundControl
        {
@@ -60,7 +65,18 @@ namespace System.Web.UI.WebControls {
                protected virtual IDataSource GetDataSource ()
                {
                        if (IsBoundUsingDataSourceID) {
-                               Control ctrl = NamingContainer.FindControl (DataSourceID);
+                               Control namingContainer;
+                               Control ctrl = null;
+
+                               namingContainer = NamingContainer;
+                               
+                               while (namingContainer != null) {
+                                       ctrl = namingContainer.FindControl (DataSourceID);
+                                       if (ctrl != null)
+                                               break;
+                                       namingContainer = namingContainer.NamingContainer;
+                               }
+
                                if (ctrl == null)
                                        throw new HttpException (string.Format ("A control with ID '{0}' could not be found.", DataSourceID));
                                if (!(ctrl is IDataSource))
@@ -131,7 +147,7 @@ namespace System.Web.UI.WebControls {
                
                protected internal override void OnLoad (EventArgs e)
                {
-                       if (IsBoundUsingDataSourceID && (!Page.IsPostBack || !EnableViewState))
+                       if (!Page.IsPostBack || (IsViewStateEnabled && !IsDataBound))
                                RequiresDataBinding = true;
 
                        base.OnLoad(e);
@@ -151,40 +167,36 @@ namespace System.Web.UI.WebControls {
                [ThemeableAttribute (false)]
                [DefaultValueAttribute ("")]
                [WebCategoryAttribute ("Data")]
-               public virtual string DataMember
-               {
-                       get {
-                               object o = ViewState["DataMember"];
-                               if(o!=null)
-                                       return (string)o;
-                               return String.Empty;
-                       }
-                       set {
-                               ViewState["DataMember"] = value;
-                       }
+               public virtual string DataMember {
+                       get { return ViewState.GetString ("DataMember", ""); }
+                       set { ViewState["DataMember"] = value; }
                }
 
                [IDReferencePropertyAttribute (typeof(DataSourceControl))]
                public override string DataSourceID {
-                       get {
-                               object o = ViewState ["DataSourceID"];
-                               if (o != null)
-                                       return (string)o;
-                               
-                               return String.Empty;
-                       }
+                       get { return ViewState.GetString ("DataSourceID", ""); }
                        set {
                                ViewState ["DataSourceID"] = value;
                                base.DataSourceID = value;
                        }
                }
                
+               // 
+               // See DataBoundControl.MarkAsDataBound msdn doc for the code example
+               // 
                protected override void PerformSelect ()
                {
-                       OnDataBinding (EventArgs.Empty);
+                       // Call OnDataBinding here if bound to a data source using the
+                       // DataSource property (instead of a DataSourceID), because the
+                       // databinding statement is evaluated before the call to GetData.       
+                       if (!IsBoundUsingDataSourceID)
+                               OnDataBinding (EventArgs.Empty);
+
                        DataSourceView view = GetData ();
-                       if (view != null)
+                       if (view != null) {
                                view.Select (SelectArguments, new DataSourceViewSelectCallback (OnSelect));
+                               MarkAsDataBound ();
+                       }
                }
                
                void OnSelect (IEnumerable data)
@@ -206,10 +218,19 @@ namespace System.Web.UI.WebControls {
                        }
                }
 
-               [MonoTODO]
+               bool IsDataBound {
+                       get {
+                               object dataBound = ViewState ["DataBound"];
+                               return dataBound != null ? (bool) dataBound : false;
+                       }
+                       set {
+                               ViewState ["DataBound"] = value;
+                       }
+               }
+
                protected void MarkAsDataBound ()
                {
-                       throw new NotImplementedException ();
+                       IsDataBound = true;
                }
        }
 }