Reputation: 24462
Imagine we have a webform page with a dynamically created checkbox and date time picker. If the checkbox is ticked then the date time picker is on the page, otherwise it isn't.
If I put the logic for dynamically creating date picker depending on the checkbox state in CreateChildControls then it works on initial load but when someone clicks on the checkbox, the lifecycle is
So at the point that the OnClick event is called the datetimepicker is already created and added to control tree.
Whats the best way to deal with this (keeping in mind a much more complex UI than described here)
a) Make datetime hidden in Checkbox_onClick (seems wasteful creating lots of controls that you might not need, then hiding some of them)
b) Same as above but either add/remove control from tree in Render (splits logic up between CreateChildControl and onClick which needs to handle both adding if not already and removing if already in)
c) If some button clicked then in OnRender clear out ALL the child controls and recreate them afresh. (seems inefficient)
d) Something else
Upvotes: 0
Views: 276
Reputation: 11955
Having all the events wired of up for you is the easiest, and setting Visible to false is also the easiest, if a control is not needed(Visible), it won't be rendered and therefore not created.
Upvotes: 2