public override void Activate(object host) { AspNetEdit.Editor.ComponentModel.DesignerHost deshost = host as AspNetEdit.Editor.ComponentModel.DesignerHost; if (deshost != null) { deshost.RootDocument.InsertFragment(Text); } else { throw new NotImplementedException("We need an interface to insert text into documents in other hosts"); } }
//Runs when item is clicked. //We should be able to avoid loading actual ToolboxItem instances and their //associated assemblies until they are activated. public override void Activate(object host) { IDesignerHost desHost = host as IDesignerHost; if (desHost == null) { throw new Exception("This ToolboxItem should not have been shown for this host. System.Drawing.Design.ToolboxItem requires a host of type System.ComponentModel.Design.IDesignerHost"); } //web controls have sample HTML that need to be deserialised //TODO: Fix WebControlToolboxItem so we don't have to mess around with type lookups and attributes here if ((item is System.Web.UI.Design.WebControlToolboxItem) && host is AspNetEdit.Editor.ComponentModel.DesignerHost) { AspNetEdit.Editor.ComponentModel.DesignerHost aspDesHost = (AspNetEdit.Editor.ComponentModel.DesignerHost)desHost; if (item.AssemblyName != null && item.TypeName != null) { //look up and register the type ITypeResolutionService typeRes = (ITypeResolutionService)aspDesHost.GetService(typeof(ITypeResolutionService)); typeRes.ReferenceAssembly(item.AssemblyName); Type controlType = typeRes.GetType(item.TypeName, true); //read the WebControlToolboxItem data from the attribute AttributeCollection atts = TypeDescriptor.GetAttributes(controlType); System.Web.UI.ToolboxDataAttribute tda = (System.Web.UI.ToolboxDataAttribute)atts[typeof(System.Web.UI.ToolboxDataAttribute)]; //if it's present if (tda != null && tda.Data.Length > 0) { //look up the tag's prefix and insert it into the data System.Web.UI.Design.IWebFormReferenceManager webRef = aspDesHost.GetService(typeof(System.Web.UI.Design.IWebFormReferenceManager)) as System.Web.UI.Design.IWebFormReferenceManager; if (webRef == null) { throw new Exception("Host does not provide an IWebFormReferenceManager"); } string aspText = String.Format(tda.Data, webRef.GetTagPrefix(controlType)); System.Diagnostics.Trace.WriteLine("Toolbox processing ASP.NET item data: " + aspText); //and add it to the document aspDesHost.RootDocument.DeserializeAndAdd(aspText); return; } } } //No ToolboxDataAttribute? Get the ToolboxItem to create the components itself item.CreateComponents(desHost); }