This old chestnut again.
My page is constructed as follows; I have a dropdownlist which is databound on first load. When the user selects a value from this, a postback is performed which then databinds a repeater control.
The ItemTemplate of this repeater control contains a placeholder control. In code behind in the ItemDataBound event of the repeater, I am addin开发者_开发百科g two controls dynamically to this placeholder, a hiddenfield and a checkbox.
When the user clicks the save button, I then want to iterate over all those dynamically created hiddenfields and checkboxes and determine their values. However when the user clicks the save button, those controls no longer exist as shown in the page trace.
I know this is a lifecycle issue and the articles I've seen on this suggest using Init methods to dynamically create your controls but I can't because of the way my page works, e.g. the repeater control only appears and binds after a value is chosen from the dropdownlist.
What do I need to do to maintain the dynamic controls through the postback caused by clicking on the save button?
The problem is when you hit the save button probabily you dont re-bind the repeater and the controls you have added at run time withint the ItemDataBound event are not longer available(because they don't exist anymore) Why don't you add those control at design time using the Eval function the set up the value of the hidden field?
You just don't create them dynamically just on the on selection change of the drop-down set visibility true or false for the repeater that will solve your problem.on post back you have to again create those control as they are Dynamically created.
精彩评论