After 3 and a half lovely (almost mostly?) years, I’ve been working in McCann Worldgroup Advertisement Agency as the software development team leader.
The time has come for me to leave the castle and explore some new territories out there. I’ve received two proposals from two distinct companies and I’m going to accept both of them !
The details will be here soon for nobodies who care.
Suppose that you’re going to create dynamic controls via LoadControl. Suppose again that, each dynamic control derives from a base class which in turn derives from System.Web.UserControl. After, LoadControl you’re willing to add it to a parent control like Panel or PlaceHolder etc.
In your scenario, you’re first calling LoadControl then get a reference to the webcontrol we have cited and you’re setting its one or few properties. That properties ofcourse uses a mechanism to persist themselves like ViewState.
Then page loads… User doing something that causes the page causes to postback to itself. Then you’re trying to add that dynamic controls to the control hierarchy so that the dynamic controls’ viewstate is available to your code again by the asp.net framework. But, there is absolute silence! No persistence had been occured?! Huh, how, why and the list goes with them…
The mistake was that in your scenario you were setting the properties first then calling Controls.Add to child loaded into the parent’s collection. If you do that your cake isn’t gonna be well. You have to follow the correct steps. The correct steps are: First call LoadControl to load the dynamic control, then add it to the collection of the parent, and then set the properties.
This way, you will see that the properties are persisting by the framework.
Good luck, comments are welcome, of course.
I have been trying to code a CMS system like the other ones. I have filled every space of the page with dynamic user controls that I have after named them as widgets, again like the other ones. And this, brang me sadness…
I’d love Microsoft if I am macozist, but I ain’t one.
A recipe for you: If you get ‘…exists in both…’ compiler error that says one of your classes are declared more than one file in the same namespace. But actually, this is a bug of ASP.net 2.0 precompiling layer.
I have tried to get rid of this and at last I could done it. Deleting the files in the temporary ASP.net directory doesn’t solve anything, even I have restarted all I can restart: IIS, VS05, Computer, my head, my coffee. It just didn’t work. The thing that solved the problem when I have renamed the name of the complained file; so everthing now seems ok.
ASP.net ain’t stable.