Click here to Skip to main content
13,832,534 members
Click here to Skip to main content
Add your own
alternative version


18 bookmarked
Posted 3 Oct 2008
Licenced CPOL

Loading Web User Controls Dynamically in ASP.NET Pages

, 3 Oct 2008
Rate this:
Please Sign up or sign in to vote.
Shows the correct way of loading and rendering web user controls dynamically in ASP.NET pages.


The principle of loading web user controls dynamically is easy, that is to use the PlaceHolder control. However, it is not easy to make dynamically loaded controls to work properly if we miss some key points. Detailed information about how to use web user controls dynamically is introduced in this article.

Problem Description

Firstly, remember to use the LoadControl method instead of just creating a new instance of the web user control. Using the new operator to create the instance of the web user control won't work because it just creates the control itself. All the child controls in the created web user control won't be initialized, as we can see in the following figure:

In the example above, the HelloWorld web user control contains a button (btnSayHello), a label (lblHello), a required field validator (rfv), and a text box (txtName). We can see that after HelloWorld has been created, no control within it is initialized.

So use the Page.LoadControl method instead of the new operator. The following figure shows the value of the members in the created control, which is much different from the ones we got previously.


After getting the control instance that is returned by the Page.LoadControl method, we can now use the PlaceHolder to render the control. The following line shows how:


Now you can render the control properly on the page. But what's next? When you want to fire server events in your loaded web user control, you'll find that everything is gone! The control totally disappears when the page is posted back.

What happens? Actually, ASP.NET pages run in the server environment, and it is a stateless object. This means once the page is generated for the client, the instance is disposed. So if we define a local variable in the page object, we cannot get its pervious value when the page is posted back because the page object is pretty new other than the one we first worked with.

In order to correctly render the control even if the page is posted back from the server, we must persist the control so that it can be re-rendered when the page is initializing or loading. It is impossible to persist the entire web user control with the view state or session because the web user control is not serializable. We can persist the virtual path of the control instead, so that the control can be recreated once the proper page events are fired. Don't worry about the state of the fields within the control, they will be maintained by the control itself.


Now we have the steps for loading and using web user controls dynamically. We can follow these steps which are briefly described below (for detailed solutions, please refer to the attached source files).

  1. Implement a private property for saving and retrieving the saved virtual path of the web user controls.
  2. Implement a private method for creating and loading the web user control according to the saved virtual path. It is important to remember to set the ID for the control; otherwise, the page won't work properly.
  3. In Page_Load event, simply call the method we created in step 2 to recreate the control.
  4. In the Button_Click event, set the private property we created in step 1 to the virtual path of the control that needs to be created and loaded; then call the method we created in step 2 to recreate and load the control.
  5. Override the Dispose method and dispose the controls.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Architect Prosource Development
China China
I have more than 13 years' experience in software development and more than 5 years' working experience in software industry. I'm the National Certified System Analyst and now I'm the consultant of China System Analyst Institution. I also got the MCP/MCAD certificate on .NET technology in the year 2004.
I'm very interested in system architect and analysis, and also interested in .NET technologies. For my blog please refer to

You may also be interested in...


Comments and Discussions

QuestionI need Help Pin
exoticankit15-Dec-13 20:06
memberexoticankit15-Dec-13 20:06 
Questionthank you Pin
DayanidhiTK2-Dec-12 1:46
memberDayanidhiTK2-Dec-12 1:46 
QuestionThanks Pin
ragavad118-Aug-12 9:46
memberragavad118-Aug-12 9:46 
GeneralMy vote of 5 Pin
Member 443048312-Jul-12 2:00
memberMember 443048312-Jul-12 2:00 
Questionhow do i get values stored in the usercontrol to Javascript Pin
chandruvelan3-Apr-11 21:44
memberchandruvelan3-Apr-11 21:44 
GeneralReally Good one. Pin
Mazkhan7722-Apr-10 0:01
memberMazkhan7722-Apr-10 0:01 
GeneralThanks Pin
Greg Chelstowski11-May-09 23:26
memberGreg Chelstowski11-May-09 23:26 
GeneralA life saver! Pin
leonbda13-Nov-08 5:50
memberleonbda13-Nov-08 5:50 
GeneralThis is how I have always done this... Pin
RK KL3-Oct-08 9:47
memberRK KL3-Oct-08 9:47 
GeneralRe: This is how I have always done this... Pin
acqy3-Oct-08 15:54
memberacqy3-Oct-08 15:54 
GeneralRe: This is how I have always done this... Pin
MehdiAnis14-Dec-08 7:28
memberMehdiAnis14-Dec-08 7:28 
GeneralRe: This is how I have always done this... Pin
Member 768108220-Feb-11 23:20
memberMember 768108220-Feb-11 23:20 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190114.1 | Last Updated 3 Oct 2008
Article Copyright 2008 by acqy
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid