0

I have a c# web application where I need to dynamically generate controls, and where the actions on the controls also affect which controls will be rendered. Imagine for example a list of buttons, where each button should remove itself (and its reference in the database). I have the feeling I don't understand something quite basic here (at least it should be)

I would like to use event handlers here. However, the question is then where should I generate the controls, in Page_Load or PreRender? If I generate them in page_load the event handlers haven't fired yet so the generated content isnt up to date yet. I can also generate it in PreRender but then the event handlers don't fire because the controls dont exist yet.

What would be the preferred way of handling this? I really want to avoid the option to modify the generated content later, it is way too error-prone in my case. I would like to generate all content from scratch, right after the events have been handled. Currently I solve this partially by examining the request.form before the generation of the controls but I have quite a lot of actions and it's not really a workable solution in quite some cases already.

4

1 回答 1

2

我通过覆盖 OnInit 创建控件

这意味着它们可用于视图状态以恢复它们的任何值,OnLoad 为时已晚。

PreRender 用于最后一分钟的事情,例如填充文本框和渲染脚本

但是请注意,您的控件必须在回发后以完全相同的方式重建以恢复值(相同的 ID 等)。

于 2012-03-06T12:36:50.750 回答