/******** * @version : 2.1.1 - Ext.NET Pro License * @author : Ext.NET, Inc. http://www.ext.net/ * @date : 2012-12-10 * @copyright : Copyright (c) 2007-2012, Ext.NET, Inc. (http://www.ext.net/). All rights reserved. * @license : See license.txt and http://www.ext.net/license/. ********/ using System; using System.Collections.Generic; using System.ComponentModel; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Ext.Net.Utilities; namespace Ext.Net { /// /// /// [Meta] public partial class UserControlLoader: AbstractComponent, ICustomConfigSerialization { /// /// /// public UserControlLoader() { } /// /// /// [Meta] [Category("3. UserControlLoader")] [DefaultValue("")] [Description("")] public virtual string Path { get { return this.State.Get("Path", ""); } set { this.State.Set("Path", value); } } /// /// /// [Meta] [Category("3. UserControlLoader")] [DefaultValue("")] [Description("")] public virtual string UserControlID { get { return this.State.Get("UserControlID", ""); } set { this.State.Set("UserControlID", value); } } /// /// /// [Meta] [Category("3. UserControlLoader")] [DefaultValue(ClientIDMode.Inherit)] [Description("")] public virtual ClientIDMode UserControlClientIDMode { get { return this.State.Get("UserControlClientIDMode", ClientIDMode.Inherit); } set { this.State.Set("UserControlClientIDMode", value); } } private UserControl uc; /// /// /// /// protected override void OnInit(EventArgs e) { base.OnInit(e); this.InitByPath(); } /// /// /// public virtual List UserControls { get { List controls = new List(); if (this.DesignMode) { return controls; } this.InitByPath(); if (this.uc != null) { controls.Add(this.uc); } controls.AddRange(this.Items); return controls; } } private void InitByPath() { if (this.uc == null && this.Path.IsNotEmpty()) { if (this.Page != null) { var path = this.Path; if (!path.StartsWith("~") && !path.StartsWith("/") && HttpContext.Current != null && HttpContext.Current.CurrentHandler is System.Web.UI.Page) { var dir = System.IO.Path.GetDirectoryName(HttpContext.Current.Request.CurrentExecutionFilePath).Replace("\\", "/"); path = dir + "/" + path; } this.uc = (UserControl)this.Page.LoadControl(path); if (this.UserControlID.IsNotEmpty()) { this.uc.ID = this.UserControlID; } #if NET40 this.uc.ClientIDMode = this.UserControlClientIDMode; #endif this.AfterUCAdd(this.uc); } else { this.uc = UserControlRenderer.LoadControl(this.Path, this.UserControlID.IsNotEmpty() ? this.UserControlID : null); #if NET40 this.uc.ClientIDMode = this.UserControlClientIDMode; #endif } } } /// /// /// public virtual void ClearCache() { this.uc = null; } #region ICustomConfigSerialization /// /// /// /// /// public string ToScript(System.Web.UI.Control owner) { return ""; } #endregion private ItemsCollection items; /// /// /// [PersistenceMode(PersistenceMode.InnerProperty)] public virtual ItemsCollection Items { get { if (this.items == null) { this.items = new ItemsCollection(); this.items.AfterItemAdd += this.AfterUCAdd; this.items.AfterItemRemove += this.AfterUCRemove; } return this.items; } } private static readonly object EventUserControlAdded = new object(); private static readonly object EventComponentAdded = new object(); public delegate void UserControlAddedEventHandler(object sender, UserControlAddedEventArgs e); public delegate void ComponentAddedEventHandler(object sender, ComponentAddedEventArgs e); /// /// /// [Category("Action")] [Description("")] public event UserControlAddedEventHandler UserControlAdded { add { this.Events.AddHandler(EventUserControlAdded, value); } remove { this.Events.RemoveHandler(EventUserControlAdded, value); } } // /// /// [Category("Action")] [Description("")] public event ComponentAddedEventHandler ComponentAdded { add { this.Events.AddHandler(EventComponentAdded, value); } remove { this.Events.RemoveHandler(EventComponentAdded, value); } } /// /// /// protected virtual void OnUserControlAdded(UserControlAddedEventArgs e) { UserControlAddedEventHandler handler = (UserControlAddedEventHandler)Events[EventUserControlAdded]; if (handler != null) { handler(this, e); } } /// /// /// protected virtual void OnComponentAdded(ComponentAddedEventArgs e) { ComponentAddedEventHandler handler = (ComponentAddedEventHandler)Events[EventComponentAdded]; if (handler != null) { handler(this, e); } } /// /// /// [Description("")] protected virtual void AfterUCAdd(UserControl item) { if (!this.Controls.Contains(item)) { this.Controls.Add(item); this.OnUserControlAdded(new UserControlAddedEventArgs(item)); if (item is IDynamicUserControl) { ((IDynamicUserControl)item).BeforeRender(); } foreach (var control in item.Controls) { var cmp = control as AbstractComponent; if (cmp != null) { cmp.ID = cmp.ID; this.OnComponentAdded(new ComponentAddedEventArgs(cmp)); } else if (control is LiteralControl || control is Literal) { continue; } else { throw new Exception(string.Format(ServiceMessages.NON_LAYOUT_CONTROL, control.GetType().ToString())); } } } } /// /// /// [Description("")] protected virtual void AfterUCRemove(UserControl item) { if (this.Controls.Contains(item)) { this.Controls.Remove(item); } } /// /// /// public IEnumerable Components { get { var userControls = this.UserControls; var cmps = new List(); foreach (var userControl in userControls) { foreach (var control in userControl.Controls) { var cmp = control as AbstractComponent; if (cmp != null) { cmps.Add(cmp); cmp.ID = cmp.ID; } else if (control is UserControlLoader) { cmps.AddRange(((UserControlLoader)control).Components); } else if (control is LiteralControl || control is Literal) { continue; } else { throw new Exception(string.Format(ServiceMessages.NON_LAYOUT_CONTROL, control.GetType().ToString())); } } } return cmps; } } } /// /// /// public class UserControlAddedEventArgs : EventArgs { public UserControlAddedEventArgs(UserControl control) { this.userControl = control; } private UserControl userControl; public UserControl UserControl { get { return this.userControl; } } } /// /// /// public class ComponentAddedEventArgs : EventArgs { public ComponentAddedEventArgs(BaseControl control) { this.control = control; } private BaseControl control; public BaseControl Control { get { return this.control; } } } }