R – building dynamic controls on postback (asp.net)

asp.netdynamic-controlspage-lifecyclepostback

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!!!

Best Answer

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.

Related Topic