Adding UserControl to a PlaceHolder.Controls collections

  • Thread starter Pavel Simsa via .NET 247
  • Start date
P

Pavel Simsa via .NET 247

Here's the problem: I have created a custom UserControl. I haveput a placeholder to my index page and now I'm trying to add theUserControl programatically. The problem is, the control doesnot show up.
Basically:
placeHolder1.Controls.Add(new Button()); works and new buttonappears in the placeholder and then I can access it without aproblem.
placeHolder1.Controls.Add(new myUserControl()); does not work,new myUserControl does not appear in the placeholder. Settingthe Visible property -"((myUserControl)placeHolder1.Controls[0]).Visible = true;" doesnot help but does not create an exception either so I presumethat the control is properly created, initialized and added tothe Controls collection of placeHolder1.
What am I doing wrong? Is there any thing special I have to do,either in the code/properties of the index page orcode/properties of the myUserControl.ascx file?
Thanks a million in advance
 
G

Guest

You cannot add your custom control via the method you arecurrently using. Try the following:

myUserControl uc = (myUserControl) LoadControl("Location of yourcontrol");
//Set the properties of the control here. i.e. uc.FireEvent +=new EventHandler(Whatever);
placeHolder1.Controls.Add(uc);

or if you don't require properties on your control to be set youcan do the following

placeHolder1.Controls.Add(LoadControl("Location of you file"));
Here's the problem: I have created a custom UserControl. I haveput a placeholder to my index page and now I'm trying to add theUserControl programatically. The problem is, the control doesnot show up.
Basically:
placeHolder1.Controls.Add(new Button()); works and new buttonappears in the placeholder and then I can access it without aproblem.
placeHolder1.Controls.Add(new myUserControl()); does not work,new myUserControl does not appear in the placeholder. Settingthe Visible property -"((myUserControl)placeHolder1.Controls[0]).Visible = true;" doesnot help but does not create an exception either so I presumethat the control is properly created, initialized and added tothe Controls collection of placeHolder1.
What am I doing wrong? Is there any thing special I have to do,either in the code/properties of the index page orcode/properties of the myUserControl.ascx file?
Thanks a million in advance
User submitted from AEWNET (http://www.aewnet.com/)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top