5,693,062 members and growing! (19,204 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Samples     Intermediate

Dynamically Loading User Control on a Webform using PlaceHolder control

By azamsharp

Dynamically loading a User Control on a webform using PlaceHolder control.
C#, Windows, .NET 1.0, .NET 1.1, .NET, ASP.NET, Visual Studio, VS.NET2002, VS.NET2003, Dev

Posted: 5 Aug 2004
Updated: 5 Aug 2004
Views: 95,420
Bookmarked: 21 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
45 votes for this Article.
Popularity: 3.23 Rating: 1.95 out of 5
28 votes, 62.2%
1
1 vote, 2.2%
2
3 votes, 6.7%
3
4 votes, 8.9%
4
9 votes, 20.0%
5

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


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.

ScreencastADay is a website committed to educate you everyday. The idea of the website is really simple. We believe that in order to be successful you need to learn something new everyday. ScreencastADay puts this thought into action by providing new screencast every day. This means you will get 7 screencasts a week and 365 screencasts a year unless of course it is a leap year in that case you get one more bonus screencast.

HighOnCoding.com www.HighOnCoding.com

ScreencastADay.com: www.ScreencastADay.com

RefactorCode: www.RefactorCode.com

GridViewGuy: GridViewGuy

My Blog:

Blog

Occupation: Web Developer
Location: United States United States

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 24 of 24 (Total in Forum: 24) (Refresh)FirstPrevNext
Questionhow to access methods and properties of Dynamically loaded controlsmemberjoeller3:42 6 Aug '08  
AnswerRe: how to access methods and properties of Dynamically loaded controlsmemberkcabrams4:56 4 Sep '08  
GeneralRe: how to access methods and properties of Dynamically loaded controlsmemberkcabrams5:09 4 Sep '08  
GeneralSolution to PostBack unload issuememberDaniel Saidi6:21 15 Jul '08  
GeneralPoormemberAntónio Barroso20:29 20 Sep '07  
GeneralThen... what happens with the evens inside the user control?memberjbmixed1:33 10 Mar '07  
GeneralApu explains how to make a squishy...memberlexodus6:36 6 Mar '07  
Questioncontrol loaded twice into one placeholder in asp.net 2.0memberPongsuriya17:12 15 Aug '06  
AnswerRe: control loaded twice into one placeholder in asp.net 2.0memberAlexander Marinov4:22 12 Oct '07  
GeneralNot Dynamic ...memberWaleedS23:37 13 May '06  
GeneralRe: Not Dynamic ...memberenteng.kabisote16:08 14 Nov '06  
GeneralRe: Not Dynamic ...memberBanaan_12322:13 17 Dec '06  
GeneralRe: Not Dynamic ...membersmithersgs5:24 25 Jan '07  
Generalload a control without a referencemembercipcipcode6:57 14 Feb '06  
GeneralHowto: load a control without a referencememberJon-W5:13 21 Mar '07  
GeneralWhat's next? Showing how to add two numbers?sussAnonymous14:34 7 Mar '05  
GeneralRe: What's next? Showing how to add two numbers?membereliakaris13:39 22 Apr '05  
GeneralRe: What's next? Showing how to add two numbers?memberJason Webb16:14 2 May '05  
GeneralRe: What's next? Showing how to add two numbers?sussSRL110:15 26 May '05  
GeneralI found this helpful.membereddie122086:49 7 Jun '05  
GeneralRe: What's next? Showing how to add two numbers?memberKarell9:32 24 Nov '05  
GeneralRe: What's next? Showing how to add two numbers?memberJason Webb16:00 24 Nov '05  
GeneralRe: What's next? Showing how to add two numbers?memberIndirection7:52 17 Mar '06  
GeneralRe: What's next? Showing how to add two numbers?membergranig6:33 30 Jan '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 5 Aug 2004
Editor: Smitha Vijayan
Copyright 2004 by azamsharp
Everything else Copyright © CodeProject, 1999-2008
Web09 | Advertise on the Code Project