Geeks With Blogs
Phil Sando

Today's little problem is that I am trying to load user controls from my codebehind like so:

 

Dim myControl As UserControl = LoadControl("~\Modules\Content.ascx")
Controls.Add(myControl)

On running the page myControl is no where to be seen. I wonder why that is? Well after a bit of thought the following come to mind...

  1. Am I using the correct code to insert the usercontrol?
  2. Is there an alternative available?
  3. Does the fact that the usercontrol has a page_load event make a difference?
  4. Does the fact that the usercontrol is being called from page_init  make a difference?
  5. Do I need to register the control in my aspx page at design time?

I'll be looking to answer these questions as the day goes on!

Here are my answers so far:

  1. Yes the code is working. I found this out by adding a static <p> tag to the ascx and found it is in fact being displayed on the page
  2. The fact that the user control has a page-load is making a difference. I have added Response.write("Test from within page_load") and this is being rendered ... twice
  3.  
  4.  
  5. No I do not have to register the control at design time when loading from codebehind

 

Posted on Thursday, May 27, 2010 11:41 AM | Back to top


Comments on this post: Programatically loading user controls

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © PhilSando | Powered by: GeeksWithBlogs.net