2004-06-03 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / BaseDataList.cs
index e1684626a65748ac9d1994079a5ebb0ec7b4e9f5..df57ff776e6ec75b6bce3aee959e088cd4150687 100755 (executable)
@@ -1,29 +1,27 @@
-/**\r
- * Namespace: System.Web.UI.WebControls\r
- * Class:     BaseDataList\r
- *\r
- * Author:  Gaurav Vaish\r
- * Maintainer: gvaish@iitk.ac.in\r
- * Contact: <my_scripts2001@yahoo.com>, <gvaish@iitk.ac.in>\r
- * Implementation: yes\r
- * Status:  100%\r
- *\r
- * (C) Gaurav Vaish (2001)\r
- */\r
+//
+// System.Web.UI.WebControls.BaseDataList.cs
+//
+// Authors:
+//   Gaurav Vaish (gvaish@iitk.ac.in)
+//   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
+//
+// (C) Gaurav Vaish (2001)
+// (C) 2003 Andreas Nahr
+//\r
 \r
 using System;\r
-using System.ComponentModel;\r
+using System.ComponentModel;
+using System.ComponentModel.Design;\r
 using System.Collections;\r
 using System.Web;\r
-using System.Web.UI;\r
+using System.Web.UI;
+using System.Web.Util;\r
 \r
 namespace System.Web.UI.WebControls\r
 {\r
        [DefaultEvent("SelectedIndexChanged")]\r
        [DefaultProperty("DataSource")]\r
-       [ParseChildren(true)]\r
-       [PersistChildren(false)]\r
-       //TODO: [Designer("??")]\r
+       [Designer("System.Web.UI.Design.WebControls.BaseDataListDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]\r
        public abstract class BaseDataList: WebControl\r
        {\r
                private  static readonly object SelectedIndexChangedEvent = new object();\r
@@ -53,7 +51,10 @@ namespace System.Web.UI.WebControls
                }\r
 \r
                public override void DataBind()\r
-               {\r
+               {
+                       #if NET_2_0
+                       RequiresDataBinding = false;
+                       #endif\r
                        OnDataBinding(EventArgs.Empty);\r
                }\r
 \r
@@ -126,7 +127,7 @@ namespace System.Web.UI.WebControls
                }\r
 \r
                [Browsable(true)]\r
-               //[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]\r
+               [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]\r
                [WebSysDescription("BaseDataList_DataKeys")]\r
                public DataKeyCollection DataKeys\r
                {\r
@@ -159,27 +160,23 @@ namespace System.Web.UI.WebControls
 \r
                [Bindable(true)]\r
                [DefaultValue(null)]\r
-               //[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]\r
+               [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]\r
                [WebCategory("Data")]\r
                [WebSysDescription("BaseDataList_DataSource")]\r
-               public virtual object DataSource\r
-               {\r
-                       get\r
-                       {\r
-                               return dataSource;\r
-                       }\r
-                       set\r
-                       {\r
-                               if( (value!=null) && (value is IListSource || value is IEnumerable) )\r
-                               {\r
-                                       dataSource = value;\r
-                               } else\r
-                               {\r
-                                       throw new ArgumentException(HttpRuntime.FormatResourceString("Invalid_DataSource_Type", ID));\r
-                               }\r
-                       }\r
-               }\r
-\r
+               public virtual object DataSource {
+                       get {
+                               return dataSource;
+                       }
+                       set {
+                               if (value == null || value is IListSource || value is IEnumerable) {
+                                       dataSource = value;
+                               } else {
+                                       throw new ArgumentException (HttpRuntime.FormatResourceString (
+                                                               "Invalid_DataSource_Type", ID));
+                               }
+                       }
+               }
+
                [Bindable(true)]\r
                [DefaultValue(GridLines.Both)]\r
                [WebCategory("Appearance")]\r
@@ -196,11 +193,14 @@ namespace System.Web.UI.WebControls
                        {\r
                                ((TableStyle)ControlStyle).GridLines = value;\r
                        }\r
-               }\r
+               }
+
+               // LAMESPEC HorizontalAlign has a Category attribute, this should obviously be a WebCategory attribute
+               // but is defined incorrectly in the MS framework\r
 \r
                [Bindable(true)]\r
                [DefaultValue(HorizontalAlign.NotSet)]\r
-               [WebCategory("Layout")]\r
+               [Category("Layout")]\r
                [WebSysDescription("BaseDataList_HorizontalAlign")]\r
                public virtual HorizontalAlign HorizontalAlign\r
                {\r
@@ -240,7 +240,7 @@ namespace System.Web.UI.WebControls
                        Controls.Clear();\r
                        if(ViewState[ItemCountViewStateKey]!=null)\r
                        {\r
-                               CreateControlHierarchy(true);\r
+                               CreateControlHierarchy(false);\r
                                ClearChildViewState();\r
                        }\r
                }\r
@@ -268,10 +268,106 @@ namespace System.Web.UI.WebControls
                protected override void Render(HtmlTextWriter writer)\r
                {\r
                        PrepareControlHierarchy();\r
-                       base.RenderContents(writer);\r
+                       RenderContents(writer);\r
                }\r
 \r
                protected abstract void PrepareControlHierarchy();\r
-               protected abstract void CreateControlHierarchy(bool useDataSource);\r
+               protected abstract void CreateControlHierarchy(bool useDataSource);
+               
+               #if NET_2_0
+
+                       
+                       // should be `internal protected' (why, oh WHY did they do that !?!)
+                       protected override void OnInit (EventArgs e)
+                       {
+                               base.OnInit(e);
+                               inited = true;
+                               if (!Page.IsPostBack)
+                                       RequiresDataBinding = true;
+                       }
+                       
+                       // should be `internal protected' (why, oh WHY did they do that !?!)
+                       protected override void OnLoad (EventArgs e)
+                       {
+                               IDataSource ds = GetDataSourceObject () as IDataSource;
+                               if (ds != null && DataSourceID != "")
+                                       ds.DataSourceChanged += new EventHandler (OnDataSourceChanged);
+                               
+                               base.OnLoad(e);
+                       }
+                       
+                       // should be `internal protected' (why, oh WHY did they do that !?!)
+                       protected override void OnPreRender (EventArgs e)
+                       {
+                               EnsureDataBound ();
+                               base.OnPreRender (e);
+                       }
+                               
+                       protected void EnsureDataBound ()
+                       {
+                               if (RequiresDataBinding && DataSourceID != "")
+                                       DataBind ();
+                       }
+                       
+                       protected virtual object GetDataSourceObject ()
+                       {
+                               if (DataSourceID != "")
+                                       return (IDataSource) NamingContainer.FindControl (DataSourceID);
+                               
+                               return DataSource;
+                       }
+                       
+                       protected virtual IEnumerable GetResolvedDataSource ()
+                       {
+                               if (DataSource != null && DataSourceID != "")
+                                       throw new HttpException ();
+                               
+                               IDataSource ds = this.GetDataSourceObject () as IDataSource;
+                               if (ds != null && DataSourceID != "")
+                                       return ds.GetView (DataMember).Select ();
+                               else if (DataSource != null)
+                                       return DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
+                               else
+                                       return null; 
+                       }
+                       
+                       protected void OnDataSourceChanged (object sender, EventArgs e)
+                       {
+                               RequiresDataBinding = true;
+                       }
+                       
+                       public virtual string DataSourceID {
+                               get {
+                                       object o = ViewState ["DataSourceID"];
+                                       if (o != null)
+                                               return (string)o;
+                                       
+                                       return String.Empty;
+                               }
+                               set {
+                                       if (inited)
+                                               RequiresDataBinding = true;
+                                       
+                                       ViewState ["DataSourceID"] = value;
+                               }
+                       }
+                       
+                       bool requiresDataBinding;
+                       protected bool RequiresDataBinding {
+                               get { return requiresDataBinding; }
+                               set { requiresDataBinding = value; }
+                       }
+                       
+                       protected bool inited;
+                               
+               #else
+                       internal IEnumerable GetResolvedDataSource ()
+                       {
+                               if (DataSource != null)
+                                       return DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
+                               else
+                                       return null; 
+                       }
+               #endif\r
        }\r
 }\r