How to load a user control dynamically in runtime?

 Posted by Raja on 7/9/2008 | Category: ASP.NET Interview questions | Views: 10983
Answer:

Write following code.

Control c = (Control)Page.LoadControl("~/usercontrol/MyUserControl.ascx");


Page.Form.Controls.Add(c);


Instead of adding this user control into the Form you can add into Panel too so that you can position it at your desired location. In that case you need to delcare a panel in the .aspx page and write following code.

Panel1.Controls.Add(c);


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Posted by: Poster on: 10/16/2008

This is exactly i was looking for. Thanks

Login to post response