[RESOLVED] how to add multiple controls to Declarative Catalog Part by default

dear experts ,

i want to add control 1, control 2 etc.. by default in Declarative Catalog Part

and using following code


DeclarativeCatalogPart dp = (DeclarativeCatalogPart)CatalogZone1.FindControl("DeclarativeCatalogPart1");
      if (!DeclarativeCatalogPart1.HasControls())
      {
     

      Control x = LoadControl("UserControls/Provider.ascx");
      x.ID = "Provider.ascx";
     

      DeclarativeCatalogPart1.Controls.Add(x);
      x = LoadControl("UserControls/Consumer.ascx");
      x.ID = "Consumer.ascx";
     

      DeclarativeCatalogPart1.Controls.Add(x);
      



      }



but it only displays last item in it

any one guide me what mistake am i doing or what is the solution to my problem?

actually i want to give default 4-5 webparts options so that user could add it to page

any help would be appreciated.

Hi,

I think the problem is this:

Control Class is a reference type in C#, so if you want to create multiple instance of usercontrol, you need to use "new" use new to invoke the new default constructor.

Like:

DeclarativeCatalogPart dp = (DeclarativeCatalogPart)CatalogZone1.FindControl("DeclarativeCatalogPart1");
      if (!DeclarativeCatalogPart1.HasControls())
      {
     
      Control x = LoadControl("UserControls/Provider.ascx");
      x.ID = "Provider.ascx";
      DeclarativeCatalogPart1.Controls.Add(x);

      x = new Control();  //create a new instance
      x = LoadControl("UserControls/Consumer.ascx");
      x.ID = "Consumer.ascx";
     
      DeclarativeCatalogPart1.Controls.Add(x);
      
 
      }

More information about the value types and reference types you can check:

http://msdn.microsoft.com/en-us/library/s1ax56ch.aspx

http://www.albahari.com/valuevsreftypes.aspx

Hope this can help you.

Where in your code is this being called?  Why do you declare a DeclarativeCatalogPart dp and then never use it?  I can add a user control to DeclarativeCatalogPart1 but it always just gets dropped and replaced but whatever controls are statically declared in the aspx file.

thoughts on "[RESOLVED] how to add multiple controls to Declarative Catalog Part by default "

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>