ASP.NET: Problem with dynamicly loaded user controls and events (and the solution)

Having problem with dynamicly loaded user controls doesn't fire the events properly, on for instance a button or linkbutton, although their on_load etc events are fired in the user control on postback so it clearly loads the control?

Your code might look like this:
myControl.Controls.Add(Page.LoadControl("~/Units/MyUserControl.ascx"));
And if so the problem is that your user control isn't assigned with an id and thus the parser won't see the loaded control as the same that fired the event (although it might sometimes). To solve it replace the code as follows:
MyUserControlClass uc=Page.LoadControl("~/Units/MyUserControl.ascx");
uc.ID="myusercontrol";
myControl.Controls.Add(uc);
That should solve the problem!! :)

Related posts:

Comments

comments powered by Disqus