Monday, January 11, 2010

Adding a UserControl to a Placeholder Dynamically

So I was trying to dynamically generate my asp.net page by newing a UserControl and adding it to a Placeholder on Page_Load:

ucSelectMed ucMySelectMed = new ucSelectMed();
phSelectMed.Controls.Add(ucMySelectMed);

Ran the project, nothing was on the page.

Was really confused because the following works:

TextBox tb = new TextBox();
phSelectMed.Controls.Add(tb);

Anyway, what was needed is:

ucSelectMed ucMySelectMed = (ucSelectMed)LoadControl("ucSelectMed.ascx");
phSelectMed.Controls.Add(ucMySelectMed);

No comments:

Post a Comment