Home > c#, Code > Dynamically creating user controls in code behind

Dynamically creating user controls in code behind

If you have a user control that you wish to dynamically add to your page the following will not work;

UserControl control = new UserControl()

The problem is that the control has not been successfully created and all the internal controls that make up the user control, like text boxes etc, will be null.

The way to create the control is with the “LoadControl()” statement.

UserControl control = LoadControl("~/controls/UserControl.ascx") as UserControl;

and now it will work.

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)

Categories: c#, Code Tags: ,
  1. No comments yet.
  1. No trackbacks yet.