Overriding the DeclarativeCatalog Web Part

Last post 05-23-2007 3:04 PM by matt_cyr. 4 replies.

Sort Posts:

  • Overriding the DeclarativeCatalog Web Part

    05-22-2007, 2:51 PM
    • Loading...
    • SBRADBERY
    • Joined on 05-22-2007, 6:32 PM
    • Posts 3

    I'm a  newbie and this might be simple but am going around in circles for now. I want to get rid of the DeclarativeCatalog Part and implement my own system for adding my custom web parts to a page. I'm aware that I can use the WebPartManager object to add Web Parts, and the syntax off the WebPartManager object is:

    WebPartManager.AddWebPart(WebPart, WebPartZone, ZoneIndex)

    My question is how to get the "handle" to the given custom Web Part. Ex: 

    <%@ Register TagPrefix="psp" TagName="ButtonBar" Src="./UserControls/ButtonBar.ascx" %>

    Dim ButtonBar1 As WebPart

    ButtonBar1 = [don't know what to put here]  (I looked into using the CreateWebPart method but seems to work only as a wrapper for non-webparts).

    wpm.AddWebPart(ButtonBar1, pspZone1,  0)

    Would appreciate a nudge in the right direction. Also, some of the custom web parts are written as user controls (like the example above), and others are written as true web parts, so it would be great to have a solution that accomodates both approaches.

    Thanks much in advance for any help....

    Filed under: ,
  • Re: Overriding the DeclarativeCatalog Web Part

    05-23-2007, 10:09 AM
    Answer
    • Loading...
    • matt_cyr
    • Joined on 03-12-2007, 9:29 AM
    • Posts 85

     You can try something like this..should work.

     

    Dim wpm as WebPartManager = WebPartManager.GetCurrentWebPartManager(Me.Page)
    Dim gwp As GenericWebPart = wpm.CreateWebPart(LoadControl("UserControls/ButtonBar.ascx"))
    wpm.AddWebPart(gwp, pspzone1, 0)
      
  • Re: Overriding the DeclarativeCatalog Web Part

    05-23-2007, 12:35 PM
    • Loading...
    • SBRADBERY
    • Joined on 05-22-2007, 6:32 PM
    • Posts 3

    Thanks Matt this was a big help....

    I'm getting this runtime error though in the bolded section below:

    "The specified control of type 'ASP.PsPortals_ButtonBar_UserControl' does not have an ID."

            Dim wpm1 As WebPartManager = WebPartManager.GetCurrentWebPartManager(Me.Page)
            Dim gwp As GenericWebPart = wpm1.CreateWebPart(LoadControl("./UserControls/ButtonBar.ascx"))
            wpm1.AddWebPart(gwp, pspZone_LogView, 0)

    I tried adding a ClassName to the UserControl, (as per example in MSDN: http://msdn2.microsoft.com/en-us/library/c0az2h86.aspx), but that did not clear the error. Do you have any idea what "ID" it is talking about?

    Thanks much again I think I'm close...

     

  • Re: Overriding the DeclarativeCatalog Web Part

    05-23-2007, 1:07 PM
    • Loading...
    • SBRADBERY
    • Joined on 05-22-2007, 6:32 PM
    • Posts 3

     Ok....I got it working - I had to break out the creation of the webpart into a seperate step and assign an ID to it. Not completely sure why, but it works just fine now. Final code was:

     Dim wpm1 As WebPartManager = WebPartManager.GetCurrentWebPartManager(Me.Page)

    ButtonBar1 = LoadControl("./UserControls/ButtonBar.ascx")

    ButtonBar1.ID = "ButtonBar"

    Dim gwp As GenericWebPart = wpm1.CreateWebPart(ButtonBar1)

    wpm1.AddWebPart(gwp, pspZone_LogView, 0)

    Thanks again

  • Re: Overriding the DeclarativeCatalog Web Part

    05-23-2007, 3:04 PM
    Answer
    • Loading...
    • matt_cyr
    • Joined on 03-12-2007, 9:29 AM
    • Posts 85

     That's what I would suggest doing.  The Control needs an ID in order to be turned into a WebPart.
    So you will have to separate the code into the 2 lines like you did.
    Same will need to be done if you are loading a webpart class as well i believe.

Page 1 of 1 (5 items)
Microsoft Communities
Page view counter