Click here to Skip to main content
15,892,059 members
Articles / Web Development / ASP.NET

ASP.NET/AJAX 3.5 With Aquarium Express

Rate me:
Please Sign up or sign in to vote.
3.60/5 (7 votes)
18 Sep 2008Ms-PL12 min read 35.7K   954   29  
Learn to build modern AJAX and ASP.NET 3.5 applications with free Aquarium Express Framework
using System;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using MyCompany.Data;
using AjaxControlToolkit;

namespace MyCompany.Web
{
	[TargetControlType(typeof(Panel))]
    [TargetControlType(typeof(HtmlContainerControl))]
    public class DataViewExtender : ExtenderControl
    {
        
        private string _controller;
        
        private string _view;
        
        private string _servicePath;
        
        private int _pageSize;
        
        private bool _showActionBar;
        
        private string _filterSource;
        
        private string _filterFields;
        
        private bool _lookupMode;
        
        private string _lookupValue;
        
        private string _lookupText;
        
        private string _lookupPostBackExpression;
        
        private bool _allowCreateLookupItems;
        
        public DataViewExtender()
        {
            this._pageSize = 10;
            this._showActionBar = true;
        }
        
        [System.ComponentModel.Description("The name of the data controller. Controllers are stored in the \"~/Controllers\" fo" +
            "lder of your project. Do not include the file extension.")]
        public string Controller
        {
            get
            {
                return _controller;
            }
            set
            {
                _controller = value;
            }
        }
        
        [System.ComponentModel.Description("The name of the startup view in the data controller. The first view is displayed " +
            "if the property is left blank.")]
        public string View
        {
            get
            {
                return _view;
            }
            set
            {
                _view = value;
            }
        }
        
        [System.ComponentModel.Description("The path to the data controller web service.")]
        [System.ComponentModel.DefaultValue("~/Services/DataControllerService.asmx")]
        public string ServicePath
        {
            get
            {
                if (String.IsNullOrEmpty(_servicePath))
                	return "~/Services/DataControllerService.asmx";
                return _servicePath;
            }
            set
            {
                _servicePath = value;
            }
        }
        
        [System.ComponentModel.Description("The number of rows displayed by grid views of the data controller.")]
        [System.ComponentModel.DefaultValue(10)]
        public int PageSize
        {
            get
            {
                return _pageSize;
            }
            set
            {
                _pageSize = value;
            }
        }
        
        [System.ComponentModel.Description("Specifies if the action bar is displayed above the views of the data controller.")]
        [System.ComponentModel.DefaultValue(true)]
        public bool ShowActionBar
        {
            get
            {
                return _showActionBar;
            }
            set
            {
                _showActionBar = value;
            }
        }
        
        [System.ComponentModel.Description(@"Defines the external source of filtering values. This may be the name of URL parameter or DHTML element in the page. Data view extender will automatically recognize if the DHTML element is also extended and will interface with the client-side extender object.")]
        public string FilterSource
        {
            get
            {
                return _filterSource;
            }
            set
            {
                _filterSource = value;
            }
        }
        
        [System.ComponentModel.Description("Specify the field(s) of the data controller that shall be filtered with the value" +
            "s from the source defined by the FilterSource property.")]
        public string FilterFields
        {
            get
            {
                return _filterFields;
            }
            set
            {
                _filterFields = value;
            }
        }
        
        [System.ComponentModel.Browsable(false)]
        public string LookupValue
        {
            get
            {
                return _lookupValue;
            }
            set
            {
                _lookupValue = value;
                _lookupMode = true;
            }
        }
        
        [System.ComponentModel.Browsable(false)]
        public string LookupText
        {
            get
            {
                return _lookupText;
            }
            set
            {
                _lookupText = value;
            }
        }
        
        [System.ComponentModel.Browsable(false)]
        public string LookupPostBackExpression
        {
            get
            {
                return _lookupPostBackExpression;
            }
            set
            {
                _lookupPostBackExpression = value;
            }
        }
        
        [System.ComponentModel.DefaultValue(true)]
        [System.ComponentModel.Browsable(false)]
        public bool AllowCreateLookupItems
        {
            get
            {
                return _allowCreateLookupItems;
            }
            set
            {
                _allowCreateLookupItems = value;
            }
        }
        
        protected override System.Collections.Generic.IEnumerable<ScriptDescriptor> GetScriptDescriptors(Control targetControl)
        {
            ScriptBehaviorDescriptor descriptor = new ScriptBehaviorDescriptor("Web.DataView", targetControl.ClientID);
            descriptor.AddProperty("id", this.ClientID);
            descriptor.AddProperty("controller", this.Controller);
            descriptor.AddProperty("viewId", this.View);
            descriptor.AddProperty("servicePath", ResolveClientUrl(ServicePath));
            descriptor.AddProperty("pageSize", this.PageSize);
            descriptor.AddProperty("showActionBar", this.ShowActionBar);
            descriptor.AddProperty("baseUrl", Page.ResolveClientUrl("~"));
            if (_lookupMode)
            {
                descriptor.AddProperty("mode", "Lookup");
                descriptor.AddProperty("lookupValue", LookupValue);
                descriptor.AddProperty("lookupText", LookupText);
                if (!(String.IsNullOrEmpty(LookupPostBackExpression)))
                	descriptor.AddProperty("lookupPostBackExpression", LookupPostBackExpression);
                if (AllowCreateLookupItems)
                	descriptor.AddProperty("newViewId", MyCompany.Data.Controller.LookupActionArgument(Controller, "New"));
            }
            if (!(String.IsNullOrEmpty(FilterSource)))
            {
                Control source = NamingContainer.FindControl(FilterSource);
                if ((source != null) && (source is DataViewExtender))
                	descriptor.AddProperty("filterSource", source.ClientID);
                else
                	descriptor.AddProperty("filterSource", this.FilterSource);
            }
            if (!(String.IsNullOrEmpty(FilterFields)))
            	descriptor.AddProperty("filterFields", this.FilterFields);
            descriptor.AddProperty("cookie", Guid.NewGuid().ToString());
            return new ScriptBehaviorDescriptor[] {
                    descriptor};
        }
        
        protected override System.Collections.Generic.IEnumerable<ScriptReference> GetScriptReferences()
        {
            List<ScriptReference> scripts = new List<ScriptReference>();
            scripts.Add(new ScriptReference("~/Scripts/Web.DataViewResources.js"));
            scripts.Add(new ScriptReference("~/Scripts/Web.DataView.js"));
            scripts.AddRange(ScriptObjectBuilder.GetScriptReferences(typeof(DropDownExtender)));
            scripts.AddRange(ScriptObjectBuilder.GetScriptReferences(typeof(ModalPopupExtender)));
            scripts.AddRange(ScriptObjectBuilder.GetScriptReferences(typeof(AlwaysVisibleControlExtender)));
            scripts.AddRange(ScriptObjectBuilder.GetScriptReferences(typeof(PopupControlExtender)));
            scripts.AddRange(ScriptObjectBuilder.GetScriptReferences(typeof(CalendarExtender)));
            return scripts;
        }
        
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            DropDownExtender dde = new DropDownExtender();
            Controls.Add(dde);
            ScriptObjectBuilder.RegisterCssReferences(dde);
            CalendarExtender ce = new CalendarExtender();
            Controls.Add(ce);
            ScriptObjectBuilder.RegisterCssReferences(ce);
            Controls.Clear();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.
This is a Organisation

1 members

Comments and Discussions