Dynamically Create controls and save the controls values across postback - ASP.Net C#

NLV picture NLV · Mar 25, 2011 · Viewed 9.6k times · Source

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 -

  1. Tab Title
  2. Tab Content
  3. Tab Footer link

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 -

  1. Recreate previously created n - 1 tab settings panel while adding a nth tab. So across each and every postback I need to recreate the settings panel for each and every tab. But I don't know how to persist the values in this case.
  2. Add the settings panel in the client side with simple html controls and on page UnLoad parse the controls -> Parse the settings -> Save it in a hidden field -> Get it in the server side. In this case I lose all the server side capabilities where I need to write validation scripts for the controls.

Any insights on how better to do this?

Answer

Richard Friend picture Richard Friend · Mar 25, 2011

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