Im using this code to create a UserControl:
Control UserControl = Page.LoadControl("/datatypes/TextString.ascx");
UserControl.ID = "CTR_" + Alias;
e.Item.FindControl("ControlHolder").Controls.Add(UserControl);
The control gets rendered on the page but the ID doesn’t get set.
What am I doing wrong?
The ID should always be set, even if you don’t set it yourself.
.Net 4 has some new mechanisms for handling IDs, but none of them will result in a missing ID. However, if your issue is that the ID has changed, this may be what you are interested in.
Also, you should verify the point in the page lifecycle that the dynamic control is being added. In order to survive postback, it must be added in the Page_Init method and it must always be added (whether it is a postback or not).