using System.Text;
using System.Web.Util;
using System.ComponentModel;
+using System.Security.Permissions;
namespace System.Web.UI.WebControls {
- [DesignerAttribute ("System.Web.UI.Design.WebControls.HierarchicalDataBoundControlDesigner, System.Design, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.IDesigner")]
+ // 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
{
+ DataSourceSelectArguments selectArguments;
+ DataSourceView currentView;
+
protected DataBoundControl ()
{
}
+
+ /* Used for controls that used to inherit from
+ * WebControl, so the tag can propagate upwards
+ */
+ internal DataBoundControl (HtmlTextWriterTag tag) : base (tag)
+ {
+ }
- protected IDataSource GetDataSource ()
+
+ protected virtual IDataSource GetDataSource ()
{
- if (DataSourceID != "")
- return NamingContainer.FindControl (DataSourceID) as IDataSource;
+ if (IsBoundUsingDataSourceID) {
+ 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))
+ throw new HttpException (string.Format ("The control with ID '{0}' is not a control of type IDataSource.", DataSourceID));
+ return (IDataSource) ctrl;
+ }
+
+ if (DataSource == null) return null;
+
+ IDataSource ds = DataSource as IDataSource;
+ if (ds != null) return ds;
+
+ IEnumerable ie = DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
+ if (ie != null) return new CollectionDataSource (ie);
- return DataSource as IDataSource;
+ throw new HttpException (string.Format ("Unexpected data source type: {0}", DataSource.GetType()));
+ }
+
+ protected virtual DataSourceView GetData ()
+ {
+ if (currentView == null)
+ UpdateViewData ();
+ return currentView;
}
- protected DataSourceView GetData ()
+ DataSourceView InternalGetData ()
{
- if (DataSource != null && DataSourceID != "")
- throw new HttpException ();
+ if (DataSource != null && IsBoundUsingDataSourceID)
+ throw new HttpException ("Control bound using both DataSourceID and DataSource properties.");
IDataSource ds = GetDataSource ();
- if (ds != null && DataSourceID != "")
+ if (ds != null)
return ds.GetView (DataMember);
else
return null;
protected override void OnDataPropertyChanged ()
{
- RequiresDataBinding = true;
+ base.OnDataPropertyChanged ();
+ UpdateViewData ();
}
protected virtual void OnDataSourceViewChanged (object sender, EventArgs e)
RequiresDataBinding = true;
}
- // should be `internal protected' (why, oh WHY did they do that !?!)
- protected override void OnLoad (EventArgs e)
+ protected override void OnPagePreLoad (object sender, EventArgs e)
{
- IDataSource ds = GetDataSource ();
- if (ds != null && DataSourceID != "")
- ds.DataSourceChanged += new EventHandler (OnDataSourceViewChanged);
-
- base.OnLoad(e);
+ base.OnPagePreLoad (sender, e);
+ UpdateViewData ();
}
- protected virtual void PerformDataBinding (IEnumerable data)
+ void UpdateViewData ()
{
- OnDataBinding (EventArgs.Empty);
+ DataSourceView view = InternalGetData ();
+ if (view == currentView) return;
+
+ if (currentView != null)
+ currentView.DataSourceViewChanged -= new EventHandler (OnDataSourceViewChanged);
+
+ currentView = view;
+
+ if (view != null)
+ view.DataSourceViewChanged += new EventHandler (OnDataSourceViewChanged);
}
+
+ protected internal override void OnLoad (EventArgs e)
+ {
+ if (!Page.IsPostBack || (IsViewStateEnabled && !IsDataBound))
+ RequiresDataBinding = true;
+ base.OnLoad(e);
+ }
+ protected internal virtual void PerformDataBinding (IEnumerable data)
+ {
+ }
+
protected override void ValidateDataSource (object dataSource)
{
- if (dataSource is IListSource || dataSource is IEnumerable)
+ if (dataSource is IListSource || dataSource is IEnumerable || dataSource is IDataSource)
return;
- throw new ArgumentException ();
+ throw new ArgumentException ("Invalid data source source type. The data source must be of type IListSource, IEnumerable or IDataSource.");
}
[ThemeableAttribute (false)]
[DefaultValueAttribute ("")]
[WebCategoryAttribute ("Data")]
- public 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(HierarchicalDataSourceControl))]
+ [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 {
- if (Initialized)
- RequiresDataBinding = true;
-
ViewState ["DataSourceID"] = value;
+ base.DataSourceID = value;
}
}
+ //
+ // See DataBoundControl.MarkAsDataBound msdn doc for the code example
+ //
protected override void PerformSelect ()
{
- IEnumerable data = null;
- DataSourceView view = GetData ();
- if (view != null)
- data = view.ExecuteSelect (SelectArguments);
- else if (DataSource != null)
- data = DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
+ // 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) {
+ view.Select (SelectArguments, new DataSourceViewSelectCallback (OnSelect));
+ MarkAsDataBound ();
+ }
+ }
+
+ void OnSelect (IEnumerable data)
+ {
PerformDataBinding (data);
+ OnDataBound (EventArgs.Empty);
+ }
+
+ protected virtual DataSourceSelectArguments CreateDataSourceSelectArguments ()
+ {
+ return DataSourceSelectArguments.Empty;
}
- [MonoTODO]
protected DataSourceSelectArguments SelectArguments {
- get { return null; }
+ get {
+ if (selectArguments == null)
+ selectArguments = CreateDataSourceSelectArguments ();
+ return selectArguments;
+ }
+ }
+
+ bool IsDataBound {
+ get {
+ object dataBound = ViewState ["DataBound"];
+ return dataBound != null ? (bool) dataBound : false;
+ }
+ set {
+ ViewState ["DataBound"] = value;
+ }
+ }
+
+ protected void MarkAsDataBound ()
+ {
+ IsDataBound = true;
}
}
}