Dynamically added controls in Asp.Net

后端 未结 9 1661
情书的邮戳
情书的邮戳 2020-12-01 02:22

I\'m trying to wrap my head around asp.net. I have a background as a long time php developer, but I\'m now facing the task of learning asp.net and I\'m having some trouble w

9条回答
  •  盖世英雄少女心
    2020-12-01 02:59

    You must add your control inside OnInit event and viewstate will be preserved. Don't use if(ispostback), because controls must be added every time, event in postback!
    (De)Serialization of viewstate happens after OnInit and before OnLoad, so your viewstate persistence provider will see dynamically added controls if they are added in OnInit.

    But in scenario you're describing, probably multiview or simple hide/show (visible property) will be better solution.
    It's because in OnInit event, when you must read dropdown and add new controls, viewstate isn't read (deserialized) yet and you don't know what did user choose! (you can do request.form(), but that feels kinda wrong)

提交回复
热议问题