Advertisement
Guest User

Designer Events

a guest
Mar 21st, 2010
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.18 KB | None | 0 0
  1. [Designer(typeof(DesignerEventsDesigner))]
  2. public class DesignerEvents : WebControl
  3. {
  4. }
  5.  
  6.  
  7. public class DesignerEventsDesigner : ControlDesigner
  8. {
  9.     private string controlAddedID, controlRemovedID;
  10.     private bool designerActive;
  11.  
  12.     public override void Initialize(IComponent component)
  13.     {
  14.         base.Initialize(component);
  15.  
  16.         //Hook our events
  17.         HookDesignerEvents();
  18.     }
  19.  
  20.     public override string GetDesignTimeHtml()
  21.     {
  22.         //Grab our control
  23.         DesignerEvents control = (Component as DesignerEvents);
  24.        
  25.         //Check to see if our control has a width, if not set it.
  26.         if (control.Width.IsEmpty) control.Width = 300;
  27.  
  28.         string html = "<div id='" + control.ID +"' style='height: " + control.Height
  29.                     + "; width: " + control.Width + "'>ControlAdded: " + controlAddedID
  30.                     + "<br>ControlRemoved: " + controlRemovedID
  31.                     + "<br>DesignerActivated: " + designerActive.ToString()
  32.                     + "</div>";
  33.         return html;
  34.     }
  35.  
  36.     public void HookDesignerEvents()
  37.     {
  38.         //Get the IDesignerHost
  39.         IDesignerHost host = Component.Site.GetService(typeof(IDesignerHost)) as IDesignerHost;
  40.  
  41.         //Get the component event service
  42.         IComponentChangeService componentService = host.GetService(typeof(IComponentChangeService)) as IComponentChangeService;
  43.  
  44.         //Hook our events
  45.         host.Activated += new EventHandler(host_Activated);
  46.         componentService.ComponentAdded += new ComponentEventHandler(componentService_ComponentAdded);
  47.         componentService.ComponentRemoved += new ComponentEventHandler(componentService_ComponentRemoved);
  48.     }
  49.  
  50.     void componentService_ComponentRemoved(object sender, ComponentEventArgs e)
  51.     {
  52.         controlRemovedID = e.Component.Site.Name;
  53.         UpdateDesignTimeHtml();
  54.     }
  55.  
  56.     void componentService_ComponentAdded(object sender, ComponentEventArgs e)
  57.     {
  58.         controlAddedID = e.Component.Site.Name;
  59.         UpdateDesignTimeHtml();
  60.     }
  61.  
  62.     void host_Activated(object sender, EventArgs e)
  63.     {
  64.         designerActive = true;
  65.         UpdateDesignTimeHtml();
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement