views:

509

answers:

2

I'm sure this has been answered but I cannot find it...perhaps because the terms are used for so many differing questions...anyway.

how is the best way to store information about building dynamic controls on postback etc in the init event...

e.g. take the classic "questionnaire" scenario...question.aspx?id=1

  1. get the id from the querystring
  2. load the questions from where ever and build controls dynamically, or dynamically add each question user control for how ever many questions etc
  3. page posts back (user presses submit I guess!)
  4. in the init event...where should you have already stored the id to recreate the question controls?

things i've tried/thought of so far:

  • I initially stored the value in viewstate as it's relevant to the page, but the viewstate value isn't available in page init (or is it??)
  • a hidden variable and then accessing the value in the form collection
  • session - I don't want to use session, seems problematic, e.g. what if the user views two questionnaires?
  • the querystring again? but that seems too easy to be changed and the dynamic controls just wouldn't make sense...
  • in the controls themselves? e.g. disregard the questionnaire id all together and simply go off the question id perhaps?? assuming that control ids would be in some format like controlQ# e.g. controlQ1, controlQ2. this means that each question id must be unique

any help/guidance etc much appreciated!!!

A: 

try to create/load the usercontrols in the page OnLoad. if you want to load inside a usercontrol other usercontrols use the OnInit event.

use the viewstate for the persistence of the information you will need to re-instantiate the usercontrols during the postback. to map the postback data to the controltree you have to ensure that you have the same control-tree. after that mapping you could change your controltree (load some other controls e.g.)

contols-id: yes, you have to give them unique ids, otherwise it will not work. if you store your questions in a database, there should not be any problem to use the database-primary key for this, so name them e.g. question-1, question-2 (the controls)

karlis
If I create them in the onLoad event, does it sound right that i'll have to manually then restore the postback values where if I created them in the init event they would automatically be restored?
davidsleeps
are we still talking on usercontrols? you don't have to manually restore the postback values. this is done automatically. but todo that, the framework first needs to know you controls. if you have your controls declared in aspx the framework will do this work for you. but if you add the controls during the lifecycle (aka dynamic) then you have to do this also in the postback.
karlis
lets say you add in the first request (get) an address control to your placeholder. the user fills some fields (street, city..). on postback the framework will assign the postdata to the controls. if there is now no control with the id which was set in the first step, you will get an exception. if you add the control before processing postback data the framework could find the control and add the data.after this, you could process you page the normal way (saving data etc.). after finishing this task, you could change your controls, so maybe you remove the address control and add a phone cont
karlis
A: 

The query string will still be available during a postback, so you could take the ID from there.

If you are concerned about users manipulating the query string parameters, then maybe you should add some additional parameters which will allow you to verify the correctness of the query string during a postback. As a simple example, you could add a MD5 hash of the ID parameter and the current session's ID, and recreate/compare that checksum during a postback.

M4N