Click here to Skip to main content
15,860,972 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 312K   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

 
GeneralMy vote of 5 Pin
eshaq21-Nov-13 12:58
eshaq21-Nov-13 12:58 
GeneralMy vote of 1 Pin
ii_noname_ii25-Mar-13 1:08
ii_noname_ii25-Mar-13 1:08 
QuestionSet "ID" Pin
Hossein Montazeri4-May-12 19:23
professionalHossein Montazeri4-May-12 19:23 
GeneralMy vote of 5 Pin
wmtimka12-Apr-12 10:11
wmtimka12-Apr-12 10:11 
Great. That what I was looking for Smile | :) Thanks
Questionmain page getting refreshed Pin
praveenkumar palla23-Feb-11 18:19
praveenkumar palla23-Feb-11 18:19 
GeneralDoesn't Work Pin
shauncollege7-Jan-11 3:48
shauncollege7-Jan-11 3:48 
GeneralMy vote of 1 Pin
Howard Richards6-Nov-10 0:28
Howard Richards6-Nov-10 0:28 
GeneralMy vote of 1 Pin
avin0379-Aug-10 1:50
avin0379-Aug-10 1:50 
GeneralMy vote of 2 Pin
NoCodeMonkey21-Jun-10 19:20
NoCodeMonkey21-Jun-10 19:20 
GeneralMy vote of 1 Pin
mynkow18-Mar-10 5:29
mynkow18-Mar-10 5:29 
Generaldynamically loading usercontrol Pin
sifad12-Jun-09 7:56
sifad12-Jun-09 7:56 
GeneralUse jQuery Pin
sjmueller22-Dec-08 20:35
sjmueller22-Dec-08 20:35 
Questionhow to access methods and properties of Dynamically loaded controls Pin
joeller6-Aug-08 2:42
professionaljoeller6-Aug-08 2:42 
AnswerRe: how to access methods and properties of Dynamically loaded controls Pin
kcabrams4-Sep-08 3:56
kcabrams4-Sep-08 3:56 
GeneralRe: how to access methods and properties of Dynamically loaded controls Pin
kcabrams4-Sep-08 4:09
kcabrams4-Sep-08 4:09 
GeneralSolution to PostBack unload issue Pin
Daniel Saidi15-Jul-08 5:21
Daniel Saidi15-Jul-08 5:21 
GeneralPoor Pin
António Barroso20-Sep-07 19:29
António Barroso20-Sep-07 19:29 
GeneralRe: Poor Pin
Dush Abe25-Feb-09 11:26
Dush Abe25-Feb-09 11:26 
QuestionThen... what happens with the evens inside the user control? Pin
jbmixed10-Mar-07 0:33
jbmixed10-Mar-07 0:33 
GeneralApu explains how to make a squishy... Pin
lexodus6-Mar-07 5:36
lexodus6-Mar-07 5:36 
Questioncontrol loaded twice into one placeholder in asp.net 2.0 Pin
Pongsuriya15-Aug-06 16:12
Pongsuriya15-Aug-06 16:12 
AnswerRe: control loaded twice into one placeholder in asp.net 2.0 Pin
Alexander Marinov12-Oct-07 3:22
Alexander Marinov12-Oct-07 3:22 
GeneralNot Dynamic ... Pin
WaleedS13-May-06 22:37
WaleedS13-May-06 22:37 
GeneralRe: Not Dynamic ... Pin
enteng.kabisote14-Nov-06 15:08
enteng.kabisote14-Nov-06 15:08 
GeneralRe: Not Dynamic ... Pin
Banaan_12317-Dec-06 21:13
Banaan_12317-Dec-06 21:13 

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.