Rebinding Repeater in an event loses its ViewState on its controls

谁都会走 提交于 2020-03-21 03:56:09

问题


I have a repeater in my page with some pre-conditions such as a checkbox list and listboxes that dictate what the datasource for the repeater brings back. A button is clicked which will databind the repeater which works fine. However if the user decides they want to add something else in or remove something they can check/uncheck some fields and then hit the button again which will rebind the repeater and change what is displayed, however the viewstate of all the current controls in the repeater will be lost.

Binding the repeater in the initialization event would not be possible because the ViewState for the checkboxlist/listbox values are not available at that point in the page lifecycle, and those values are required in order to pass as parameters into my datasource for the repeater.

What are my options for maintaining the state of my repeater controls?


回答1:


repeater control binding fully recreates all of children controls inside repeater templates. Because new DataBind result may (or may not) contains extremely different data inside repeater. What kind of controls and their state you want to maintain inside repeater? Maybe using ordinal html controls and operating with theirs through Request.Form collection would be a better way?

If you want to save data from repeater before applying new databinding, best place for making it is a Page.PreRender event. In this event all Page controls already recreated and their viewstate already restored. So you may iterate by Repeater.Items collection and save data from repeater row by row. And after saving all the data you may rebind repeater controls according current filter values from page.



来源:https://stackoverflow.com/questions/20106148/rebinding-repeater-in-an-event-loses-its-viewstate-on-its-controls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!