Click here to Skip to main content
6,305,776 members and growing! (16,921 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, ASP.NET, VS.NET2003, Dev
Posted:5 Aug 2004
Views:111,736
Bookmarked:27 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
48 votes for this article.
Popularity: 3.44 Rating: 2.05 out of 5
28 votes, 58.3%
1
1 vote, 2.1%
2
3 votes, 6.3%
3
5 votes, 10.4%
4
11 votes, 22.9%
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


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

Occupation: Web Developer
Location: United States United States

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 27 (Total in Forum: 27) (Refresh)FirstPrevNext
Generaldynamically loading usercontrol Pinmembersifad8:56 12 Jun '09  
GeneralUse jQuery Pinmembersjmueller21:35 22 Dec '08  
Questionhow to access methods and properties of Dynamically loaded controls Pinmemberjoeller3:42 6 Aug '08  
AnswerRe: how to access methods and properties of Dynamically loaded controls Pinmemberkcabrams4:56 4 Sep '08  
GeneralRe: how to access methods and properties of Dynamically loaded controls Pinmemberkcabrams5:09 4 Sep '08  
GeneralSolution to PostBack unload issue PinmemberDaniel Saidi6:21 15 Jul '08  
GeneralPoor PinmemberAntónio Barroso20:29 20 Sep '07  
GeneralRe: Poor PinmemberMember 368254812:26 25 Feb '09  
GeneralThen... what happens with the evens inside the user control? Pinmemberjbmixed1:33 10 Mar '07  
GeneralApu explains how to make a squishy... Pinmemberlexodus6:36 6 Mar '07  
Questioncontrol loaded twice into one placeholder in asp.net 2.0 PinmemberPongsuriya17:12 15 Aug '06  
AnswerRe: control loaded twice into one placeholder in asp.net 2.0 PinmemberAlexander Marinov4:22 12 Oct '07  
GeneralNot Dynamic ... PinmemberWaleedS23:37 13 May '06  
GeneralRe: Not Dynamic ... Pinmemberenteng.kabisote16:08 14 Nov '06  
GeneralRe: Not Dynamic ... PinmemberBanaan_12322:13 17 Dec '06  
GeneralRe: Not Dynamic ... Pinmembersmithersgs5:24 25 Jan '07  
Generalload a control without a reference Pinmembercipcipcode6:57 14 Feb '06  
GeneralHowto: load a control without a reference PinmemberJon-W5:13 21 Mar '07  
GeneralWhat's next? Showing how to add two numbers? PinsussAnonymous14:34 7 Mar '05  
GeneralRe: What's next? Showing how to add two numbers? Pinmembereliakaris13:39 22 Apr '05  
GeneralRe: What's next? Showing how to add two numbers? PinmemberJason Webb16:14 2 May '05  
GeneralRe: What's next? Showing how to add two numbers? PinsussSRL110:15 26 May '05  
GeneralI found this helpful. Pinmembereddie122086:49 7 Jun '05  
GeneralRe: What's next? Showing how to add two numbers? PinmemberKarell9:32 24 Nov '05  
GeneralRe: What's next? Showing how to add two numbers? PinmemberJason Webb16:00 24 Nov '05  

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-2009
Web12 | Advertise on the Code Project