I want to add a user control when the user clicks on a bu开发者_如何学Pythontton (the server-side event).
I'm able to dynamically add the control but since it's too late on the page life cycle the events on that user control won't fire (in this specific case, a button in that user control).All solutions I've seen regarding this is to add the control on Page_Init (or Page_Load) something that I can't do here, also the control must stay "dynamic" so I can't just hide it and then make it visible.
The problem here was the fact that the IDs of such controls were auto generated and asp.net matches the events of the controls by ID.
I fixed this by giving a ID to each dynamic control after they are created.
Control control = LoadControl("~/Controls/" + controlName + ".ascx");
control.ID = "SomeId";
parentElement.Controls.Add(control);
精彩评论