//------------------------------------------------------------------------------ // // Copyright (c) Microsoft Corporation. All rights reserved. // //------------------------------------------------------------------------------ namespace System.Web.UI.WebControls { using System; using System.Collections; using System.Web.UI; /// /// The data source used by DataBoundControls like GridView/FormView to support data methods /// to perform the Delete, Insert, Select, and Update operations. /// public class ModelDataSource : IDataSource, IStateManager { internal const string DefaultViewName = "DefaultView"; private ModelDataSourceView _view; private ICollection _viewNames; private EventHandler DataSourceChanged; public ModelDataSource(Control dataControl) { if (dataControl == null) { throw new ArgumentNullException("dataControl"); } DataControl = dataControl; } /// /// Updates the required properties for the one way data binding to work. /// public void UpdateProperties(string modelTypeName, string selectMethod) { UpdateProperties(modelTypeName, selectMethod, String.Empty, String.Empty, String.Empty, String.Empty); } /// /// Updates the required properties for the two way data binding to work. /// public void UpdateProperties(string modelTypeName, string selectMethod, string updateMethod, string insertMethod, string deleteMethod, string dataKeyName) { View.UpdateProperties(modelTypeName, selectMethod, updateMethod, insertMethod, deleteMethod, dataKeyName); } /// /// Control for which this is an internal data source to support the data methods for data operations. /// public Control DataControl { get; private set; } public event CallingDataMethodsEventHandler CallingDataMethods { add { View.CallingDataMethods += value; } remove { View.CallingDataMethods -= value; } } /// /// The default (and only) ModelDataSourceView for this ModelDataSource. /// Subclasses can override this method to return a different ModelDataSourceView. /// public virtual ModelDataSourceView View { get { if (_view == null) { _view = new ModelDataSourceView(this); } return _view; } } protected virtual bool IsTrackingViewState() { return ((IStateManager)View).IsTrackingViewState; } protected virtual void LoadViewState(object savedState) { ((IStateManager)View).LoadViewState(savedState); } protected virtual object SaveViewState() { return ((IStateManager)View).SaveViewState(); } protected virtual void TrackViewState() { ((IStateManager)View).TrackViewState(); } /// /// Gets the view associated with this data source. /// private DataSourceView GetView(string viewName) { //Ignore the input viewName as there is only a single view. return View; } /// /// private ICollection GetViewNames() { if (_viewNames == null) { _viewNames = new string[1] { DefaultViewName }; } return _viewNames; } #region Implementation of IDataSource /// /// Raised when the underlying data source has changed. The /// change may be due to a change in the control's properties, /// or a change in the data due to an edit action performed by /// the DataSourceControl. /// event EventHandler IDataSource.DataSourceChanged { add { DataSourceChanged += value; } remove { DataSourceChanged -= value; } } /// DataSourceView IDataSource.GetView(string viewName) { return GetView(viewName); } /// ICollection IDataSource.GetViewNames() { return GetViewNames(); } #endregion #region Implementation of IStateManager bool IStateManager.IsTrackingViewState { get { return IsTrackingViewState(); } } void IStateManager.LoadViewState(object savedState) { LoadViewState(savedState); } object IStateManager.SaveViewState() { return SaveViewState(); } void IStateManager.TrackViewState() { TrackViewState(); } #endregion } }