How to dynamically refresh a .NET databound repeater control

不问归期 提交于 2019-12-07 01:10:27

问题


I have a .NET repeater control that is data-bound to a List. As part of the Repeater's Item Collection, I have a "Remove Button" that effectively removes this current List element.

This works, in code-behind I can successfully remove an item from the datasource of the Repeater.

My problem is this : when I reset the updated datasource and call MyRepeater.DataBind() again, the Repeater interface does not refresh with the Item removed.

I am looking for the event to essentially redraw or refresh the Repeater based on the updated List. Thanks for any pointers or examples.


回答1:


You need to call the 'DataBind' method on your datasource, then call 'DataBind' on your Repeater control.




回答2:


Are you feeding the refreshed data source?

If you are setting data source in code-behind, you need to set it with refreshed data then call DataBind method.




回答3:


I ran into something like that with a Repeater Control and a DataTable source.

There wasn't a Refresh method in DataTable, but calling DataTable.EnableDynamicData(typeof(DataTable)) on initial page load solved the problem.




回答4:


I had a similar situation...a repeater bound to an xmlDataSource, both inside an UpdatePanel. I wanted to let the user type in one name at a time then click an "Add" button to update the list in the repeater.

I set "EnableViewState" to False on the repeater and the xmlDataSource, and set "EnableCaching" on the xmlDataSource to False as well. I set the Data property of the xmlDataSource, called DataBind for the xmlDataSource, set the DataSourceID property of the repeater, then called DataBind for the repeater. Maybe that was overkill...but it worked. Maybe this will help.

UPDATE: I found that by setting EnableViewState to False on the repeater control, my ItemCommand event would not fire. But I think you only need to set EnableViewState/EnableCaching to False for the data source...I have returned the EnableViewState setting to True for the repeater and now all seems well.




回答5:


Forcing Databind is normally done where the automatic DataBind is done in the PreRender event.

Normally if you did the remove in the click event, the repeater should refresh by itself since automatically in the preRender, controls on page are DataBind(). Here is what the doc of microsoft say :

PreRender : Before this event occurs each data bound control whose DataSourceID property is set calls its DataBind method.

source

So probably you affected Youritem.DataSource = List, but MS suggest doing YourItem.DataSourceID = List.ID, or something like that.

Hope it helps



来源:https://stackoverflow.com/questions/463317/how-to-dynamically-refresh-a-net-databound-repeater-control

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