We are dynamically adding ItemTemplate and HeaderTemplate to WebDropDown at runtime, but when the page is posted back, the templates are lost.
If we re-add the templates anywhere in the page lifecycle after oninit, then the data is lost. If we do not re-add the templates, the data is not lost.
However, since the configuration for the templates is stored in viewstate and ensuretemplates is called during OnInit, we cannot use either OnInit or EnsureTemplates to configure the templates.
Moving the template definition into declarative markup is not an option because our customers are allowed to control the visibility, captions, and other information associated with the WebDropDowns.
Can someone please give us some guidance on how we can add these templates programatically and have them and the associated data survive postbacks?
Dear Karl
Thank you for contacting Infragistics!
We received your support request, and this case has been assigned to me. Infragistics is dedicated to helping you solve this issue.
I am looking into this issue and will get back to you by the end of the day Thursday with more information or questions for you.
Best regards,
Martin Pavlov
Infragistics, Inc.
support@infragistics.com
After examining the control source code, I was able to devise the following solution:
Create a new control that inherits from WebDropDown.
Override the CreateChildControls method and before calling the base implementation, assign the header and item templates (this takes care of the initial page loading).
Override the LoadViewState method and after calling the base implementation (to ensure viewstate is loaded since we cache information there), assign the templates to the control. Once this has been completed call the base implementation of EnsureTemplates.
These steps ensure that programatically created header and item templates that depend on viewstate for configuration correctly survive a postback event.
Hello Karl,
Thank you for sharing your solution with our community.
I hope that this answer will be helpful for other users too.