Reputation: 21641
Consider this -
I allow the end user to create a tab control dynamically in an ASP.Net page by getting some details. For each and every tab added I get some settings -
So I get these details for each and every tab. The user is provided with a button 'Add Tab' to more one more tab. So I need to add one more tab settings panel in the page to get the tab settings. But doing so, I lose the values entered in the previously created dynamic tab settings panel. The user can add as many tabs and enter settings for each and every tab. Finally when they save it I build the tab control (based on their settings and content) and I render the control.
Since the controls are dynamic, I'm able to thing of two options -
Any insights on how better to do this?
Upvotes: 5
Views: 9782
Reputation: 2976
One method is to save data that you need to recreate the tabs in the ViewState. You can do this during the PreRender event for example. On Postback you need to recreate the controls you had originally, maintaining their original IDs and their order in the hierarchy. You can do this during the LoadViewState phase.
Once in Page_Load you can create your new tab
Upvotes: 1
Reputation: 16018
I would add the controls server-side, just remember that you need to re-create all controls on every postback
You could save your details of any controls that need to be created in viewstate then build your control tree from that in CreateChildControls
Upvotes: 2
Reputation: 25994
I've implemented option #1 in the past. You'll have to recreate your controls tree at a certain point in page events lifecicle, so that when view event to restore viewstate kicks in, it has all the controls it needs. As far as I remember, you also need to restore all the controls in exactly same hierarchy ans with identical names it existed when page was rendered and view stated persisted, prior to be sent to the client. If there's any discrepancy, loading viewstate will not work for dynamic controls.
Upvotes: 0