Hur var det med viewState för dynamiskt laddade UserControls. Från http://msdn2.microsoft.com/en-us/library/ms972975.aspx#usercontrols_topic10Dynamiskt laddade UserControls och ViewState
Finns det idag något smidigt sätt att behålla dess state eller måste jag helt enkelt ladda in kontrollen igen? Sv: Dynamiskt laddade UserControls och ViewState RTFM ;)
Kort svar:
"As with dynamically loading Web controls, when dynamically loading a user control it must be done on every visit to the page (including postbacks). Too, the ideal place for this loading is in the Page's Init event."
// Hoppas det hjälper :)
Mer info
"There are some subtle issues involving the Page's ViewState that can arise when adding Web controls to a Web page dynamically. A thorough discussion of this is beyond the scope of this article. The short of it is, though, that the controls must be reloaded on each and every postback, and the safest place to dynamically add the Web controls is during the Init event. For more information on dynamically adding Web controls see HOW TO: Dynamically Create Controls in ASP.NET with Visual Basic .NET and Adding Controls to a Web Forms Page Programmatically.
Dynamically loading a user control differs slightly from dynamically loading a Web control. Before a user control can be loaded into a Controls collection, it must first be loaded using the LoadControl(string) method. LoadControl(string) takes in as a string input the virtual path to the user control to load (you can use the ~ syntax discussed earlier). It returns a Control instance of the user control. This Control instance can then be added to the Page's Controls collection:"
// Load the User Control
Control uc = LoadControl("~/MyUserControl.ascx");
// Add the User Control to the Controls collection
Page.Controls.Add(uc);
"As with dynamically loading Web controls, when dynamically loading a user control it must be done on every visit to the page (including postbacks). Too, the ideal place for this loading is in the Page's Init event."