开发者

building dynamic controls on postback (asp.net)

开发者 https://www.devze.com 2022-12-16 20:48 出处:网络
I\'m sure this has been answered but I cannot find it...perhaps because the terms are used for so many differing questions...anyway.

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


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.


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消