Skip to main content
Email Password   helpLost your password?

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:

<%@ 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.

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.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Generaldynamically loading usercontrol Pin
sifad
8:56 12 Jun '09  
GeneralUse jQuery Pin
sjmueller
21:35 22 Dec '08  
Questionhow to access methods and properties of Dynamically loaded controls Pin
joeller
3:42 6 Aug '08  
AnswerRe: how to access methods and properties of Dynamically loaded controls Pin
kcabrams
4:56 4 Sep '08  
GeneralRe: how to access methods and properties of Dynamically loaded controls Pin
kcabrams
5:09 4 Sep '08  
GeneralSolution to PostBack unload issue Pin
Daniel Saidi
6:21 15 Jul '08  
GeneralPoor Pin
António Barroso
20:29 20 Sep '07  
GeneralRe: Poor Pin
Member 3682548
12:26 25 Feb '09  
GeneralThen... what happens with the evens inside the user control? Pin
jbmixed
1:33 10 Mar '07  
GeneralApu explains how to make a squishy... Pin
lexodus
6:36 6 Mar '07  
Questioncontrol loaded twice into one placeholder in asp.net 2.0 Pin
Pongsuriya
17:12 15 Aug '06  
AnswerRe: control loaded twice into one placeholder in asp.net 2.0 Pin
Alexander Marinov
4:22 12 Oct '07  
GeneralNot Dynamic ... Pin
WaleedS
23:37 13 May '06  
GeneralRe: Not Dynamic ... Pin
enteng.kabisote
16:08 14 Nov '06  
GeneralRe: Not Dynamic ... Pin
Banaan_123
22:13 17 Dec '06  
GeneralRe: Not Dynamic ... Pin
smithersgs
5:24 25 Jan '07  
Generalload a control without a reference Pin
cipcipcode
6:57 14 Feb '06  
GeneralHowto: load a control without a reference Pin
Jon-W
5:13 21 Mar '07  
GeneralWhat's next? Showing how to add two numbers? Pin
Anonymous
14:34 7 Mar '05  
GeneralRe: What's next? Showing how to add two numbers? Pin
eliakaris
13:39 22 Apr '05  
GeneralRe: What's next? Showing how to add two numbers? Pin
Jason Webb
16:14 2 May '05  
GeneralRe: What's next? Showing how to add two numbers? Pin
SRL1
10:15 26 May '05  
GeneralI found this helpful. Pin
eddie12208
6:49 7 Jun '05  
GeneralRe: What's next? Showing how to add two numbers? Pin
Karell
9:32 24 Nov '05  
GeneralRe: What's next? Showing how to add two numbers? Pin
Jason Webb
16:00 24 Nov '05  


Last Updated 5 Aug 2004 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009