maandag 16 juni 2008

Persisting dynamic controls through postbacks (part 2 of 2)

Second way: Adding it through the ID's of the controls

This way is a lot easier than the first way of doing it. It's a way that I've learnt from a colleague of mine (Koen Roos - His blog can be found here: ).

This method is based on the ID's of the controls.
When you add your controls to the page, you have to save the ID of the control into the viewstate instead of the serialized control itself. 
During postback we read out the ID's of the objects and re-add new controls, but with the same ID's as the old controls.
ASP.NET remembers the properties of these controls et voila.. the controls are persisted.
This is especially useful for f.e. fileinputs, where you can't set the selected file property by code for security reasons.

Step 1: Add a new Label to the page
Step 2: Re-add these labels during postbacks in the OnLoad event


Done! :)

Geen opmerkingen: