Click here to Skip to main content
Licence 
First Posted 5 Aug 2004
Views 210,228
Bookmarked 37 times

Dynamically Loading User Control on a Webform using PlaceHolder control

By | 5 Aug 2004 | Article
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:

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

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

About the Author

azamsharp

Web Developer

United States United States

Member

I am the founder of knowledge base website, HighOnCoding, GridViewGuy, RefactorCode.com and ScreencastADay.com.
 
HighOnCoding is a website which will get you high legally with useful information. There are tons of articles, videos and podcasts hosted on HighOnCoding.
 
HighOnCoding.com www.HighOnCoding.com
 

My Blog:

Blog

 

Buy my iPhone app ABC Pop


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionSet "ID" PinmemberHossein Montazeri19:23 4 May '12  
GeneralMy vote of 5 Pinmemberwmtimka10:11 12 Apr '12  
Questionmain page getting refreshed Pinmemberpraveenkumar palla18:19 23 Feb '11  
GeneralDoesn't Work Pinmembershauncollege3:48 7 Jan '11  
GeneralMy vote of 1 PinmemberHoward Richards0:28 6 Nov '10  
GeneralMy vote of 1 Pinmemberavin0371:50 9 Aug '10  
GeneralMy vote of 2 PinmemberRyan Leeder19:20 21 Jun '10  
GeneralMy vote of 1 Pinmembermynkow5:29 18 Mar '10  
Generaldynamically loading usercontrol Pinmembersifad7:56 12 Jun '09  
GeneralUse jQuery Pinmembersjmueller20:35 22 Dec '08  
Questionhow to access methods and properties of Dynamically loaded controls Pinmemberjoeller2:42 6 Aug '08  
AnswerRe: how to access methods and properties of Dynamically loaded controls Pinmemberkcabrams3:56 4 Sep '08  
GeneralRe: how to access methods and properties of Dynamically loaded controls Pinmemberkcabrams4:09 4 Sep '08  
GeneralSolution to PostBack unload issue PinmemberDaniel Saidi5:21 15 Jul '08  
GeneralPoor PinmemberAntónio Barroso19:29 20 Sep '07  
GeneralRe: Poor PinmemberMember 368254811:26 25 Feb '09  
QuestionThen... what happens with the evens inside the user control? Pinmemberjbmixed0:33 10 Mar '07  
GeneralApu explains how to make a squishy... Pinmemberlexodus5:36 6 Mar '07  
Questioncontrol loaded twice into one placeholder in asp.net 2.0 PinmemberPongsuriya16:12 15 Aug '06  
I have a project that has been developed using ASP.Net V1.1. In my code file I have load the control using "PlaceHolder.Control.Add(myControl)". After that I convert this project with VWD 2005. In runtime, my webpage seem to have the same behavior both V1.1 and V2.0. But after I copy the aspx file and rename it both aspx and code-file. When it executes, it loads the control twice into one placeholder. I don't know what happen and don't know how to solve this problem. Does anyone have any idea?Confused | :confused:
AnswerRe: control loaded twice into one placeholder in asp.net 2.0 PinmemberAlexander Marinov3:22 12 Oct '07  
GeneralNot Dynamic ... PinmemberWaleedS22:37 13 May '06  
GeneralRe: Not Dynamic ... Pinmemberenteng.kabisote15:08 14 Nov '06  
GeneralRe: Not Dynamic ... PinmemberBanaan_12321:13 17 Dec '06  
GeneralRe: Not Dynamic ... Pinmembersmithersgs4:24 25 Jan '07  
GeneralRe: Not Dynamic ... PinmemberRitesh Totlani0:05 20 Aug '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.5.120529.1 | Last Updated 6 Aug 2004
Article Copyright 2004 by azamsharp
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid