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

Dynamically Loading User Control on a Webform using PlaceHolder control

By azamsharp | 5 Aug 2004
Dynamically loading a User Control on a webform using PlaceHolder control.
30 votes, 53.6%
1
2 votes, 3.6%
2
3 votes, 5.4%
3
6 votes, 10.7%
4
15 votes, 26.8%
5
2.17/5 - 56 votes
μ 2.20, σa 3.10 [?]

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
Questionmain page getting refreshed Pinmemberpraveenkumar palla19:19 23 Feb '11  
GeneralDoesn't Work Pinmembershauncollege4:48 7 Jan '11  
GeneralMy vote of 1 PinmemberHoward Richards1:28 6 Nov '10  
GeneralMy vote of 1 Pinmemberavin0372:50 9 Aug '10  
GeneralMy vote of 2 PinmemberRyan Leeder20:20 21 Jun '10  
GeneralMy vote of 1 Pinmembermynkow6:29 18 Mar '10  
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  
This is helpful but how do you access the methods and property of the control from the page. I need to load a different control depending on a selection made by the user. I have buttons on the page whose click events call methods on the controls that process the data selected by the user. I thought I could register one control on the page then call usercontrol.LoadControl(path) to redirect the registered control to a different ascx file. However, instead the initially registered control persists in loading. I then tried to instantiate the control in code, but then the page will not compile because the methods being called by the buttons on the page do not yet exist. Does anyone have any ideas?
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  
QuestionThen... 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  
GeneralRe: Not Dynamic ... PinmemberRitesh Totlani1:05 20 Aug '10  
Generalload a control without a reference Pinmembercipcipcode6:57 14 Feb '06  
GeneralHowto: load a control without a reference PinmemberJon-W5:13 21 Mar '07  

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
Web02 | 2.5.120210.1 | Last Updated 6 Aug 2004
Article Copyright 2004 by azamsharp
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid