LinqDataSource.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ DotNET / DotNET / 8.0 / untmp / Orcas / RTM / ndp / fx / src / xsp / System / Web / Extensions / ui / webcontrols / LinqDataSource.cs / 2 / LinqDataSource.cs

                            //------------------------------------------------------------------------------ 
// 
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//----------------------------------------------------------------------------- 

namespace System.Web.UI.WebControls { 
    using System.Collections; 
    using System.Collections.ObjectModel;
    using System.ComponentModel; 
    using System.Diagnostics.CodeAnalysis;
    using System.Drawing;
    using System.Drawing.Design;
    using System.Globalization; 
    using System.Security.Permissions;
    using System.Web.Resources; 
    using System.Web.UI; 

    // Represents a data source that applies LINQ expressions against a business object in order to perform the Select 
    // operation.  When the Delete, Insert and Update operations are enabled the business object, specified in
    // ContextTypeName, must be a LINQ TO SQL DataContext.  The LINQ expressions are applied in the order of Where,
    // OrderBy, GroupBy, OrderGroupsBy, Select.
    [ 
    AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal),
    AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal), 
    DefaultEvent("Selecting"), 
    DefaultProperty("ContextTypeName"),
    Designer("System.Web.UI.Design.WebControls.LinqDataSourceDesigner, " + AssemblyRef.SystemWebExtensionsDesign), 
    ParseChildren(true),
    PersistChildren(false),
    ResourceDescription("LinqDataSource_Description"),
    ResourceDisplayName("LinqDataSource_DisplayName"), 
    ToolboxBitmap(typeof(LinqDataSource), "LinqDataSource.ico")
    ] 
    public class LinqDataSource : DataSourceControl { 

        private const string DefaultViewName = "DefaultView"; 

        private LinqDataSourceView _view;
        private ICollection _viewNames;
        private readonly new IPage _page; 

        public LinqDataSource() { 
        } 

        // internal constructor that takes mocks for unit tests. 
        internal LinqDataSource(IDynamicQueryable dynamicQueryable, ILinqToSql linqToSql) {
            View = new LinqDataSourceView(this, DefaultViewName, Context, dynamicQueryable, linqToSql);
        }
 
        // internal constructor that takes page mock for unit tests.
        internal LinqDataSource(IPage page) { 
            _page = page; 
        }
 
        [
        DefaultValue(false),
        Category("Behavior"),
        ResourceDescription("LinqDataSource_AutoGenerateOrderByClause"), 
        ]
        public bool AutoGenerateOrderByClause { 
            get { 
                return View.AutoGenerateOrderByClause;
            } 
            set {
                View.AutoGenerateOrderByClause = value;
            }
        } 

        [ 
        DefaultValue(false), 
        Category("Behavior"),
        ResourceDescription("LinqDataSource_AutoGenerateWhereClause"), 
        ]
        public bool AutoGenerateWhereClause {
            get {
                return View.AutoGenerateWhereClause; 
            }
            set { 
                View.AutoGenerateWhereClause = value; 
            }
        } 

        [
        DefaultValue(true),
        Category("Behavior"), 
        ResourceDescription("LinqDataSource_AutoPage"),
        ] 
        public bool AutoPage { 
            get {
                return View.AutoPage; 
            }
            set {
                View.AutoPage = value;
            } 
        }
 
        [ 
        DefaultValue(true),
        Category("Behavior"), 
        ResourceDescription("LinqDataSource_AutoSort"),
        ]
        public bool AutoSort {
            get { 
                return View.AutoSort;
            } 
            set { 
                View.AutoSort = value;
            } 
        }

        [
        DefaultValue(""), 
        Category("Data"),
        ResourceDescription("LinqDataSource_ContextTypeName"), 
        ] 
        public string ContextTypeName {
            get { 
                return View.ContextTypeName;
            }
            set {
                View.ContextTypeName = value; 
            }
        } 
 
        [
        DefaultValue(null), 
        MergableProperty(false),
        PersistenceMode(PersistenceMode.InnerProperty),
        Category("Data"),
        ResourceDescription("LinqDataSource_DeleteParameters"), 
        Browsable(false)
        ] 
        public ParameterCollection DeleteParameters { 
            get {
                return View.DeleteParameters; 
            }
        }

        [ 
        DefaultValue(false),
        Category("Behavior"), 
        ResourceDescription("LinqDataSource_EnableDelete"), 
        ]
        public bool EnableDelete { 
            get {
                return View.EnableDelete;
            }
            set { 
                View.EnableDelete = value;
            } 
        } 

        [ 
        DefaultValue(false),
        Category("Behavior"),
        ResourceDescription("LinqDataSource_EnableInsert"),
        ] 
        public bool EnableInsert {
            get { 
                return View.EnableInsert; 
            }
            set { 
                View.EnableInsert = value;
            }
        }
 
        [
        DefaultValue(false), 
        Category("Behavior"), 
        ResourceDescription("LinqDataSource_EnableUpdate"),
        ] 
        public bool EnableUpdate {
            get {
                return View.EnableUpdate;
            } 
            set {
                View.EnableUpdate = value; 
            } 
        }
 
        [
        DefaultValue(""),
        Category("Data"),
        ResourceDescription("LinqDataSource_GroupBy"), 
        ]
        public string GroupBy { 
            get { 
                return View.GroupBy;
            } 
            set {
                View.GroupBy = value;
            }
        } 

        [ 
        DefaultValue(null), 
        MergableProperty(false),
        PersistenceMode(PersistenceMode.InnerProperty), 
        Category("Data"),
        ResourceDescription("LinqDataSource_GroupByParameters"),
        Browsable(false)
        ] 
        public ParameterCollection GroupByParameters {
            get { 
                return View.GroupByParameters; 
            }
        } 

        [
        DefaultValue(null),
        MergableProperty(false), 
        PersistenceMode(PersistenceMode.InnerProperty),
        Category("Data"), 
        ResourceDescription("LinqDataSource_InsertParameters"), 
        Browsable(false)
        ] 
        public ParameterCollection InsertParameters {
            get {
                return View.InsertParameters;
            } 
        }
 
        internal IPage IPage { 
            get {
                if (_page != null) { 
                    return _page;
                }
                else {
                    Page page = Page; 
                    if (page == null) {
                        throw new InvalidOperationException(AtlasWeb.Common_PageCannotBeNull); 
                    } 
                    return new PageWrapper(page);
                } 
            }
        }

        [ 
        DefaultValue(""),
        Category("Data"), 
        ResourceDescription("LinqDataSource_OrderBy"), 
        ]
        public string OrderBy { 
            get {
                return View.OrderBy;
            }
            set { 
                View.OrderBy = value;
            } 
        } 

        [ 
        DefaultValue(null),
        MergableProperty(false),
        PersistenceMode(PersistenceMode.InnerProperty),
        Category("Data"), 
        ResourceDescription("LinqDataSource_OrderByParameters"),
        Browsable(false) 
        ] 
        public ParameterCollection OrderByParameters {
            get { 
                return View.OrderByParameters;
            }
        }
 
        [
        DefaultValue(""), 
        Category("Data"), 
        ResourceDescription("LinqDataSource_OrderGroupsBy"),
        ] 
        public string OrderGroupsBy {
            get {
                return View.OrderGroupsBy;
            } 
            set {
                View.OrderGroupsBy = value; 
            } 
        }
 
        [
        DefaultValue(null),
        MergableProperty(false),
        PersistenceMode(PersistenceMode.InnerProperty), 
        Category("Data"),
        ResourceDescription("LinqDataSource_OrderGroupsByParameters"), 
        Browsable(false) 
        ]
        public ParameterCollection OrderGroupsByParameters { 
            get {
                return View.OrderGroupsByParameters;
            }
        } 

        [ 
        DefaultValue(""), 
        Category("Data"),
        ResourceDescription("LinqDataSource_Select"), 
        ]
        public string Select {
            get {
                return View.SelectNew; 
            }
            set { 
                View.SelectNew = value; 
            }
        } 

        [
        DefaultValue(null),
        MergableProperty(false), 
        PersistenceMode(PersistenceMode.InnerProperty),
        Category("Data"), 
        ResourceDescription("LinqDataSource_SelectParameters"), 
        Browsable(false)
        ] 
        public ParameterCollection SelectParameters {
            get {
                return View.SelectNewParameters;
            } 
        }
 
        [ 
        DefaultValue(true),
        Category("Behavior"), 
        ResourceDescription("LinqDataSource_StoreOriginalValuesInViewState"),
        ]
        public bool StoreOriginalValuesInViewState {
            get { 
                return View.StoreOriginalValuesInViewState;
            } 
            set { 
                View.StoreOriginalValuesInViewState = value;
            } 
        }

        [
        DefaultValue(""), 
        Category("Data"),
        ResourceDescription("LinqDataSource_TableName"), 
        ] 
        public string TableName {
            get { 
                return View.TableName;
            }
            set {
                View.TableName = value; 
            }
        } 
 
        [
        DefaultValue(null), 
        MergableProperty(false),
        PersistenceMode(PersistenceMode.InnerProperty),
        Category("Data"),
        ResourceDescription("LinqDataSource_UpdateParameters"), 
        Browsable(false)
        ] 
        public ParameterCollection UpdateParameters { 
            get {
                return View.UpdateParameters; 
            }
        }

        internal LinqDataSourceView View { 
            get {
                if (_view == null) { 
                    View = new LinqDataSourceView(this, DefaultViewName, Context); 
                }
                return _view; 
            }
            private set {
                _view = value;
                if ((_view != null) && IsTrackingViewState) { 
                    ((IStateManager)_view).TrackViewState();
                } 
            } 
        }
 
        [
        DefaultValue(""),
        Category("Data"),
        ResourceDescription("LinqDataSource_Where"), 
        ]
        public string Where { 
            get { 
                return View.Where;
            } 
            set {
                View.Where = value;
            }
        } 

        [ 
        DefaultValue(null), 
        MergableProperty(false),
        PersistenceMode(PersistenceMode.InnerProperty), 
        Category("Data"),
        ResourceDescription("LinqDataSource_WhereParameters"),
        Browsable(false)
        ] 
        public ParameterCollection WhereParameters {
            get { 
                return View.WhereParameters; 
            }
        } 

        [
        Category("Data"),
        ResourceDescription("LinqDataSource_ContextCreated"), 
        ]
        public event EventHandler ContextCreated { 
            add { 
                View.ContextCreated += value;
            } 
            remove {
                View.ContextCreated -= value;
            }
        } 

        [ 
        Category("Data"), 
        ResourceDescription("LinqDataSource_ContextCreating"),
        ] 
        public event EventHandler ContextCreating {
            add {
                View.ContextCreating += value;
            } 
            remove {
                View.ContextCreating -= value; 
            } 
        }
 
        [
        Category("Data"),
        ResourceDescription("LinqDataSource_ContextDisposing"),
        ] 
        public event EventHandler ContextDisposing {
            add { 
                View.ContextDisposing += value; 
            }
            remove { 
                View.ContextDisposing -= value;
            }
        }
 
        [
        Category("Data"), 
        ResourceDescription("LinqDataSource_Deleted"), 
        ]
        public event EventHandler Deleted { 
            add {
                View.Deleted += value;
            }
            remove { 
                View.Deleted -= value;
            } 
        } 

        [ 
        Category("Data"),
        ResourceDescription("LinqDataSource_Deleting"),
        ]
        public event EventHandler Deleting { 
            add {
                View.Deleting += value; 
            } 
            remove {
                View.Deleting -= value; 
            }
        }

        [ 
        Category("Data"),
        ResourceDescription("LinqDataSource_Inserted"), 
        ] 
        public event EventHandler Inserted {
            add { 
                View.Inserted += value;
            }
            remove {
                View.Inserted -= value; 
            }
        } 
 
        [
        Category("Data"), 
        ResourceDescription("LinqDataSource_Inserting"),
        ]
        public event EventHandler Inserting {
            add { 
                View.Inserting += value;
            } 
            remove { 
                View.Inserting -= value;
            } 
        }

        [
        Category("Data"), 
        ResourceDescription("LinqDataSource_Selected"),
        ] 
        public event EventHandler Selected { 
            add {
                View.Selected += value; 
            }
            remove {
                View.Selected -= value;
            } 
        }
 
        [ 
        Category("Data"),
        ResourceDescription("LinqDataSource_Selecting"), 
        ]
        public event EventHandler Selecting {
            add {
                View.Selecting += value; 
            }
            remove { 
                View.Selecting -= value; 
            }
        } 

        [
        Category("Data"),
        ResourceDescription("LinqDataSource_Updated"), 
        ]
        public event EventHandler Updated { 
            add { 
                View.Updated += value;
            } 
            remove {
                View.Updated -= value;
            }
        } 

        [ 
        Category("Data"), 
        ResourceDescription("LinqDataSource_Updating"),
        ] 
        public event EventHandler Updating {
            add {
                View.Updating += value;
            } 
            remove {
                View.Updating -= value; 
            } 
        }
 
        public int Delete(IDictionary keys, IDictionary oldValues) {
            return View.Delete(keys, oldValues);
        }
 
        protected override DataSourceView GetView(string viewName) {
            if (viewName == null) { 
                throw new ArgumentNullException("viewName"); 
            }
            // viewName comes from the DataMember property on the databound control and is an empty string 
            // by default.  An empty string should be treated as if it were the default view name.
            if ((viewName.Length != 0) &&
                !String.Equals(viewName, DefaultViewName, StringComparison.OrdinalIgnoreCase)) {
                throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, 
                    AtlasWeb.LinqDataSource_InvalidViewName, ID, DefaultViewName), "viewName");
            } 
            return View; 
        }
 
        protected override ICollection GetViewNames() {
            if (_viewNames == null) {
                _viewNames = new ReadOnlyCollection(new string[] { DefaultViewName });
            } 
            return _viewNames;
        } 
 
        public int Insert(IDictionary values) {
            return View.Insert(values); 
        }

        private void LoadCompleteEventHandler(object sender, EventArgs e) {
            WhereParameters.UpdateValues(Context, this); 
            OrderByParameters.UpdateValues(Context, this);
            GroupByParameters.UpdateValues(Context, this); 
            OrderGroupsByParameters.UpdateValues(Context, this); 
            SelectParameters.UpdateValues(Context, this);
        } 

        protected override void LoadViewState(object savedState) {
            if (savedState == null) {
                base.LoadViewState(null); 
            }
            else { 
                Pair myState = (Pair)savedState; 
                base.LoadViewState(myState.First);
                if (myState.Second != null) { 
                    ((IStateManager)View).LoadViewState(myState.Second);
                }
            }
        } 

        [SuppressMessage("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers", MessageId = "0#")] 
        protected internal override void OnInit(EventArgs e) { 
            base.OnInit(e);
            IPage.LoadComplete += new EventHandler(LoadCompleteEventHandler); 
            if (StoreOriginalValuesInViewState && (EnableUpdate || EnableDelete)) {
                IPage.RegisterRequiresViewStateEncryption();
            }
        } 

        [SuppressMessage("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers", MessageId = "0#")] 
        protected internal override void OnUnload(EventArgs e) { 
            base.OnUnload(e);
            // keeping the data context alive until Unload so that users can use deferred query evaluation. 
            if (_view != null) {
                _view.ReleaseContext();
            }
        } 

        protected override object SaveViewState() { 
            Pair myState = new Pair(); 
            myState.First = base.SaveViewState();
            if (_view != null) { 
                myState.Second = ((IStateManager)_view).SaveViewState();
            }
            if ((myState.First == null) &&
                (myState.Second == null)) { 
                return null;
            } 
            return myState; 
        }
 
        protected override void TrackViewState() {
            base.TrackViewState();
            if (_view != null) {
                ((IStateManager)_view).TrackViewState(); 
            }
        } 
 
        public int Update(IDictionary keys, IDictionary values, IDictionary oldValues) {
            return View.Update(keys, values, oldValues); 
        }

    }
} 


// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK