Sunday, October 01, 2006

Dynamically add webusercontrol to webform

On a few pages I dynamically add webusercontrols to a webform. It's very easy.

Drag a panel on your webform, name is default, panel1.
Make a webusercontrol in the directory "webcontrols" in your project, and name it wucmyControl.ascx, for example.

Then use this code in the load event of the form:
Dim myControl = LoadControl("~/webcontrols/wucmyControl.ascx")
'assign a value to the properties of the webcontrol
myControl.property1 = "any value"
myControl.property2 = "any value"
' add the control to the panel control
Panel1.Controls.Add(ThemaControl)

Done!

No comments: