Dynamically Adding ASP.NET Web Forms Controls Loses State on Page Refresh
I am creating dynamic label and textbox based on the number of values from the database for the selected item of the dropdownlist. Then the dynamic labels will have the names and the text box with the values. To retain the values of these controls im using Page_init event. So im using cache to hold the select开发者_如何学运维teditem from the dropdownlist.
Problem: The process is going fine. But if i try to refresh the page no items are selected in the dropdown list but the cache is not getting clear so using this cache value dynamic control are creating.
Unfortunately, dynamically added controls are not retained so you have to add them on every page load. So you will have to reload from the db, or store the total number of controls and recreate using the index on page init. ViewState will be reloaded into those controls to retain the previous value.
It's just that it's unfortunate that you have to do the work, but that is the case for dynamic controls. As an alternative, you could use a repeater which would retain the list of items that are bound to it.
HTH.
精彩评论