Click here to Skip to main content
15,867,453 members
Articles / Web Development / ASP.NET
Article

Dynamically Loading User Control on a Webform using PlaceHolder control

Rate me:
Please Sign up or sign in to vote.
2.38/5 (63 votes)
5 Aug 20041 min read 312.1K   40   38
Dynamically loading a User Control on a webform using PlaceHolder control.

Introduction

In this article, I will explain how to load UserControls programmatically on your webform. First, drag and drop a PlaceHolder control on your webform on which you will load the actual UserControl.

Adding Directive on a Webform

First, you need to add a directive on a webform telling the webform that you are going to dynamically load the usercontrol. So in the webform, add the following directive:

ASP.NET
<%@ Reference Control = "WebUserControl1.ascx" %>

Once this directive has place, you need to load the actual control in the webform. You can load the control anywhere in the form. A good place will be the Page_Init event. In the example, I have loaded the control in the Page_Load event.

C#
if(!Page.IsPostBack) 
{ 
    WebUserControl1 uc = 
      (WebUserControl1) Page.LoadControl("WebUserControl1.ascx"); 
    PlaceHolder1.Controls.Add(uc); 
}

Here in the code, WebUserControl1 is the name of the UserControl class through which we have created an instance namely "uc". The Page.LoadControl loads the control on the page. And finally the place holder control adds the control to its collection and displays it on the webform.

Conclusion

You can always use drag and drop features of the usercontrol to include it on the page. But sometimes it's important to load when some event occurs in the application, and for that you can create the controls dynamically.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
My name is Mohammad Azam and I have been developing iOS applications since 2010. I have worked as a lead mobile developer for VALIC, AIG, Schlumberger, Baker Hughes, Blinds.com and The Home Depot. I have also published tons of my own apps to the App Store and even got featured by Apple for my app, Vegetable Tree. I highly recommend that you check out my portfolio. At present I am working as a lead instructor at DigitalCrafts.




I also have a lot of Udemy courses which you can check out at the following link:
Mohammad Azam Udemy Courses

Comments and Discussions

 
GeneralRe: Not Dynamic ... Pin
smithersgs25-Jan-07 4:24
smithersgs25-Jan-07 4:24 
GeneralRe: Not Dynamic ... Pin
Ritesh Totlani20-Aug-10 0:05
Ritesh Totlani20-Aug-10 0:05 
Generalload a control without a reference Pin
cipcipcode14-Feb-06 5:57
cipcipcode14-Feb-06 5:57 
GeneralHowto: load a control without a reference Pin
Jon-W21-Mar-07 4:13
Jon-W21-Mar-07 4:13 
QuestionWhat's next? Showing how to add two numbers? Pin
Anonymous7-Mar-05 13:34
Anonymous7-Mar-05 13:34 
AnswerRe: What's next? Showing how to add two numbers? Pin
eliakaris22-Apr-05 12:39
eliakaris22-Apr-05 12:39 
AnswerRe: What's next? Showing how to add two numbers? Pin
Jason Webb2-May-05 15:14
Jason Webb2-May-05 15:14 
GeneralRe: What's next? Showing how to add two numbers? Pin
SRL126-May-05 9:15
sussSRL126-May-05 9:15 
Well if its for newbies he could have explained things a little more - placeholders for instance. I think you need to put in a little more work than this before it can be called an article.
GeneralI found this helpful. Pin
eddie122087-Jun-05 5:49
eddie122087-Jun-05 5:49 
GeneralRe: What's next? Showing how to add two numbers? Pin
Karell24-Nov-05 8:32
Karell24-Nov-05 8:32 
GeneralRe: What's next? Showing how to add two numbers? Pin
Jason Webb24-Nov-05 15:00
Jason Webb24-Nov-05 15:00 
GeneralRe: What's next? Showing how to add two numbers? Pin
Indirection17-Mar-06 6:52
Indirection17-Mar-06 6:52 
GeneralRe: What's next? Showing how to add two numbers? Pin
granig30-Jan-07 5:33
granig30-Jan-07 5:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.