Geeks With Blogs
Athens's Id Permanent Resident of the Blue Nowhere

This is the source for a recently created custom declarativecatalogpart that filters out webparts that already exist on the page. This prevents duplicate webparts from being added. Otherwise it will run from either a template control list or declared controls. 

/// <summary>
 /// A replacement for the declarative catalog part meant for limiting the webparts on a page to a single instance by hiding the webparts that already exist on the page.
 /// </summary>
 [Designer ( "System.Web.UI.Design.WebControls.WebParts.DeclarativeCatalogPartDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" ), AspNetHostingPermission ( SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal ), AspNetHostingPermission ( SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal )]
 public class CustomCatalog : CatalogPart
 {
  // Fields
  private WebPartDescriptionCollection _descriptions;
  private string _webPartsListUserControlPath;
  private ITemplate _webPartsTemplate;
  private Dictionary<string, string> _loadedWebparts;   
  private Dictionary<string, WebPart> _availableWebparts;   // <WebPartDescription.ID, WebPart>

  // Methods
  private void AddControlToDescriptions ( Control control, ArrayList descriptions, Dictionary<string, WebPart> availableWebparts)
  {
   WebPart webPart = control as WebPart;

   if ( ( webPart == null ) && !( control is LiteralControl ))    
     webPart = base.WebPartManager.CreateWebPart ( control );


   if ( ( webPart != null ) && ( ( base.WebPartManager == null ) || base.WebPartManager.IsAuthorized ( webPart ) ) )
   {
    if ( this._loadedWebparts.Count == 0 || this._loadedWebparts == null )
    {
      this.LoadExistingWebParts ();
    }

    WebPartDescription description = new WebPartDescription ( webPart );
    
    if ( !( ((Dictionary<string, string>)HttpContext.Current.Session["loadedWebParts"]).ContainsKey ( description.Title ) ))
    {
      descriptions.Add ( description );
    }
    availableWebparts.Add ( description.ID, webPart );
   }
  }

  public override WebPartDescriptionCollection GetAvailableWebPartDescriptions ()
  {
   if ( this._loadedWebparts == null || this._loadedWebparts.Count == 0 )
   {
     this.LoadExistingWebParts ();
   }
   if ( this._descriptions == null )         
    this.LoadAvailableWebParts ();

   return this._descriptions;
  }

  public override WebPart GetWebPart ( WebPartDescription description )
  {
   if ( description == null )    
    throw new ArgumentNullException ( "description" );


   if ( !this.GetAvailableWebPartDescriptions ().Contains ( description ) )
    throw new ArgumentException ( "CatalogPart_UnknownDescription", "description" );


   ((Dictionary<string, string>)HttpContext.Current.Session["loadedWebParts"]).Add ( description.Title, description.ID );

   
   return this._availableWebparts[description.ID];
  }


  public void LoadExistingWebParts ()
  {
   WebPartDescription description = null;
   if ( HttpContext.Current.Session["loadedWebParts"] != null )
    this._loadedWebparts = ( Dictionary<string, string> ) HttpContext.Current.Session["loadedWebParts"];

   if ( this._loadedWebparts == null || this._loadedWebparts.Count == 0)
    this._loadedWebparts = new Dictionary<string, string> ();

    WebPartCollection webparts = base.WebPartManager.WebParts;
    foreach ( WebPart webPart in webparts )
    {
     description = new WebPartDescription ( webPart );
     if ( !( this._loadedWebparts.ContainsKey ( description.Title ) ) && description.Title != "" )
      this._loadedWebparts.Add ( description.Title, description.ID );

    }
   if(this._loadedWebparts.Count > 0)
    HttpContext.Current.Session["loadedWebParts"] = this._loadedWebparts;
  }


  private void LoadAvailableWebParts ()
  {
   ArrayList descriptions = new ArrayList ();
   Dictionary<string, WebPart> availableWebparts = new Dictionary<string, WebPart> ();

   if ( this.WebPartsTemplate != null )
   {
    Control container = new CustomNonParentingControl ();
    this.WebPartsTemplate.InstantiateIn ( container );
    if ( container.HasControls () )
    {
     Control[] array = new Control[container.Controls.Count];
     container.Controls.CopyTo ( array, 0 );
     foreach ( Control control2 in array )
     {
      this.AddControlToDescriptions ( control2, descriptions, availableWebparts );
     }
    }
   }
   string webPartsListUserControlPath = this.WebPartsListUserControlPath;

   if ( !string.IsNullOrEmpty ( webPartsListUserControlPath ) && !base.DesignMode )
   {
    Control control3 = this.Page.LoadControl ( webPartsListUserControlPath );
    if ( ( control3 != null ) && control3.HasControls () )
    {
     Control[] controlArray2 = new Control[control3.Controls.Count];
     control3.Controls.CopyTo ( controlArray2, 0 );
     foreach ( Control control4 in controlArray2 )
     {
      this.AddControlToDescriptions ( control4, descriptions, availableWebparts );
     }
    }
   }
   this._availableWebparts = availableWebparts;
   this._descriptions = new WebPartDescriptionCollection ( descriptions );
  }


  protected override void Render ( HtmlTextWriter writer )
  {
  }

  #region Properties
  // Properties
  [EditorBrowsable ( EditorBrowsableState.Never ), Themeable ( false ), Browsable ( false )]
  public override string AccessKey
  {
   get
   {
    return base.AccessKey;
   }
   set
   {
    base.AccessKey = value;
   }
  }

  [Themeable ( false ), Browsable ( false ), EditorBrowsable ( EditorBrowsableState.Never )]
  public override Color BackColor
  {
   get
   {
    return base.BackColor;
   }
   set
   {
    base.BackColor = value;
   }
  }

  [Browsable ( false ), Themeable ( false ), EditorBrowsable ( EditorBrowsableState.Never )]
  public override string BackImageUrl
  {
   get
   {
    return base.BackImageUrl;
   }
   set
   {
    base.BackImageUrl = value;
   }
  }

  [Browsable ( false ), EditorBrowsable ( EditorBrowsableState.Never ), Themeable ( false )]
  public override Color BorderColor
  {
   get
   {
    return base.BorderColor;
   }
   set
   {
    base.BorderColor = value;
   }
  }

  [Browsable ( false ), Themeable ( false ), EditorBrowsable ( EditorBrowsableState.Never )]
  public override BorderStyle BorderStyle
  {
   get
   {
    return base.BorderStyle;
   }
   set
   {
    base.BorderStyle = value;
   }
  }

  [Themeable ( false ), EditorBrowsable ( EditorBrowsableState.Never ), Browsable ( false )]
  public override Unit BorderWidth
  {
   get
   {
    return base.BorderWidth;
   }
   set
   {
    base.BorderWidth = value;
   }
  }

  [Themeable ( false ), Browsable ( false ), EditorBrowsable ( EditorBrowsableState.Never )]
  public override string CssClass
  {
   get
   {
    return base.CssClass;
   }
   set
   {
    base.CssClass = value;
   }
  }

  [Themeable ( false ), EditorBrowsable ( EditorBrowsableState.Never ), Browsable ( false )]
  public override string DefaultButton
  {
   get
   {
    return base.DefaultButton;
   }
   set
   {
    base.DefaultButton = value;
   }
  }

  [EditorBrowsable ( EditorBrowsableState.Never ), Browsable ( false ), Themeable ( false )]
  public override ContentDirection Direction
  {
   get
   {
    return base.Direction;
   }
   set
   {
    base.Direction = value;
   }
  }

  [EditorBrowsable ( EditorBrowsableState.Never ), Themeable ( false ), Browsable ( false )]
  public override bool Enabled
  {
   get
   {
    return base.Enabled;
   }
   set
   {
    base.Enabled = value;
   }
  }

  [Browsable ( false ), Themeable ( false ), DefaultValue ( false ), EditorBrowsable ( EditorBrowsableState.Never )]
  public override bool EnableTheming
  {
   get
   {
    return false;
   }
   set
   {
    throw new NotSupportedException ( "Theming Not Supported" );
   }
  }

  [EditorBrowsable ( EditorBrowsableState.Never ), Themeable ( false ), Browsable ( false )]
  public override FontInfo Font
  {
   get
   {
    return base.Font;
   }
  }

  [EditorBrowsable ( EditorBrowsableState.Never ), Browsable ( false ), Themeable ( false )]
  public override Color ForeColor
  {
   get
   {
    return base.ForeColor;
   }
   set
   {
    base.ForeColor = value;
   }
  }

  [Browsable ( false ), Themeable ( false ), EditorBrowsable ( EditorBrowsableState.Never )]
  public override string GroupingText
  {
   get
   {
    return base.GroupingText;
   }
   set
   {
    base.GroupingText = value;
   }
  }

  [Browsable ( false ), Themeable ( false ), EditorBrowsable ( EditorBrowsableState.Never )]
  public override Unit Height
  {
   get
   {
    return base.Height;
   }
   set
   {
    base.Height = value;
   }
  }

  [Browsable ( false ), EditorBrowsable ( EditorBrowsableState.Never ), Themeable ( false )]
  public override HorizontalAlign HorizontalAlign
  {
   get
   {
    return base.HorizontalAlign;
   }
   set
   {
    base.HorizontalAlign = value;
   }
  }

  [Browsable ( false ), Themeable ( false ), EditorBrowsable ( EditorBrowsableState.Never )]
  public override ScrollBars ScrollBars
  {
   get
   {
    return base.ScrollBars;
   }
   set
   {
    base.ScrollBars = value;
   }
  }

  [DefaultValue ( "" ), Themeable ( false ), EditorBrowsable ( EditorBrowsableState.Never ), Browsable ( false )]
  public override string SkinID
  {
   get
   {
    return string.Empty;
   }
   set
   {
    throw new NotSupportedException ( "SkinID Not Supported" );
   }
  }

  [Browsable ( false ), Themeable ( false ), EditorBrowsable ( EditorBrowsableState.Never )]
  public override short TabIndex
  {
   get
   {
    return base.TabIndex;
   }
   set
   {
    base.TabIndex = value;
   }
  }

  [DefaultValueAttribute ( "DeclarativeCatalogPart_PartTitle" )]
  public override string Title
  {
   get
   {
    string text = ( string ) this.ViewState["Title"];
    if ( text == null )
    {
     return "Unknown";
    }
    return text;
   }
   set
   {
    this.ViewState["Title"] = value;
   }
  }

  [Browsable ( false ), EditorBrowsable ( EditorBrowsableState.Never ), Themeable ( false )]
  public override string ToolTip
  {
   get
   {
    return base.ToolTip;
   }
   set
   {
    base.ToolTip = value;
   }
  }

  [EditorBrowsable ( EditorBrowsableState.Never ), Themeable ( false ), Browsable ( false )]
  public override bool Visible
  {
   get
   {
    return base.Visible;
   }
   set
   {
    base.Visible = value;
   }
  }

  [DefaultValue ( "" ), Editor ( "System.Web.UI.Design.UserControlFileEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof ( UITypeEditor ) ), CategoryAttribute ( "Behavior" ), Themeable ( false ), UrlProperty, DescriptionAttribute ( "DeclarativeCatlaogPart_WebPartsListUserControlPath" )]
  public string WebPartsListUserControlPath
  {
   get
   {
    if ( this._webPartsListUserControlPath == null )
    {
     return string.Empty;
    }
    return this._webPartsListUserControlPath;
   }
   set
   {
    this._webPartsListUserControlPath = value;
    this._descriptions = null;
   }
  }

  [TemplateContainer ( typeof ( DeclarativeCatalogPart ) ), Browsable ( false ), DefaultValue ( ( string ) null ), PersistenceMode ( PersistenceMode.InnerProperty )]
  public ITemplate WebPartsTemplate
  {
   get
   {
    return this._webPartsTemplate;
   }
   set
   {
    this._webPartsTemplate = value;
    this._descriptions = null;
   }
  }

  [Themeable ( false ), EditorBrowsable ( EditorBrowsableState.Never ), Browsable ( false )]
  public override Unit Width
  {
   get
   {
    return base.Width;
   }
   set
   {
    base.Width = value;
   }
  }

  [Themeable ( false ), Browsable ( false ), EditorBrowsable ( EditorBrowsableState.Never )]
  public override bool Wrap
  {
   get
   {
    return base.Wrap;
   }
   set
   {
    base.Wrap = value;
   }
  }
  #endregion
 }

 public class CustomNonParentingControl : Control
 {
  protected override void AddedControl ( Control control, int index ){}
  protected override void RemovedControl ( Control control ){}
 }

Posted on Tuesday, August 21, 2007 1:54 PM WebParts , ASP.NET 2.0 | Back to top


Comments on this post: Custom DeclarativeCatalogPart

# re: Custom DeclarativeCatalogPart
Requesting Gravatar...
Hi, i've tried your custom declarative catalog part, and i've run into some trouble.
Here is my scenario.

<Panel>
<asp.net Ajax Update Panel>
<normal catalog zone>
<normal pagecatalogpart>
<Custom DeclarativeCatalogPart>
<web usercontrol 1>
<web usercontrol 2>
</Custom DeclarativeCatalogPart>
</normal pagecatalogpart>
</normal catalog zone>
</asp.net Ajax Update Panel>
</panel>

The catalog zone is shown when the user click on a "Add Webpart" link button, the catalog zone is shown with a asp.net ajax modalpopup extender.

When the user checked a web usercontrol 1 and add it to a webpart zone, the custom declarative catalog won't refresh (showing the 2 user control), even i try pressing "Ctrl+ F5" and go to other pages. The only way the catalog list will refresh and show only usercontrol 2 is i close the web browser and reopen it again.

Please do provide me with suggestions on how to fix this?
Thanks in advance.
Left by kaimera on Jul 01, 2008 2:10 AM

Your comment:
 (will show your gravatar)


Copyright © Athenz | Powered by: GeeksWithBlogs.net