I need to create the following functionality, and looking for the best way to store info on multiple postbacks:
Say I have a TextBox, that is related to a question, i.e. “Places I’ve visited”. Once TextBox will be loaded on first load. Under that, there is a button to “Add another place”. This will postback, which will then add another TextBox underneath the first. This can be done x number of times.
The question I have is, how do I store/remember the number of controls that have been added to the page by the user, considering that this load needs to be done in the Init event, and ViewState is not loaded at this point?
EDIT*:
I forgot to mention, that when the user saves, there would be some validation, so if validation fails, need to show all the TextBoxes, with their posted data.
After some thinking, and considering the answers given, came up with this solution.
In the controls placeholder, have a hidden input field which will store the number of controls added to the page. Then, on Init, I can have the following code (testing only):
Of course the only issue with this, is that the value could be manipulated by the user in the hidden field. The only other option would be to use Session, which I do not want to use as it sticks around, whereby if the page is refreshed this way, the form will reset itself, which is what should happen.