简体   繁体   中英

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 selectteditem 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM