Thursday, April 21, 2011

Loading User Control dynamically in a asp.net webpage

First create user control as in your project ,for reference I am naming it as “WebUserControl.ascx”

Open the aspx page where you want to show the control. Here I am going to show the control when the user clicks on the button on my page. For this first add a reference of the user control “WebUserControl.ascx ‘” as shown below to my aspx page.

Once this is done simply drag a placeholder control from the tool box and add to the aspx page. Also drag a button to the aspx page, so that we can dynamically load the user control on button click.


Once you had added the Placeholder and the button, your aspx page will look as below


Go to the code behind page of you webpage and in the button click event and add the below code


Now run your website and click on the button on the page, you can see that the user control is dynamically loaded to your webpage.