如何动态刷新 .NET 数据绑定转发器控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/463317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to dynamically refresh a .NET databound repeater control
提问by Ash Machine
I have a .NET repeatercontrol that is data-boundto a List. As part of the Repeater's Item Collection, I have a "Remove Button" that effectively removes this current List element.
我有一个列表.NET repeater控件data-bound。作为中继器的一部分Item Collection,我有一个“删除按钮”,可以有效地删除当前的列表元素。
This works, in code-behind I can successfully remove an item from the datasourceof the Repeater.
这个作品,在代码隐藏我可以成功地从删除项目datasource的的中继器。
My problem is this :when I reset the updated datasourceand call MyRepeater.DataBind()again, the Repeaterinterface does not refresh with the Item removed.
我的问题是这样的:当我重置更新datasource并MyRepeater.DataBind()再次调用时,Repeater界面不会随着项目的删除而刷新。
I am looking for the event to essentially redraw or refresh the Repeaterbased on the updated List. Thanks for any pointers or examples.
我正在寻找基于更新后的列表重绘或刷新中继器的事件。感谢您提供任何指示或示例。
采纳答案by Dhaust
You need to call the 'DataBind' method on your datasource, thencall 'DataBind' on your Repeater control.
您需要在数据源上调用“DataBind”方法,然后在 Repeater 控件上调用“DataBind”。
回答by Brian Kim
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.
如果您在代码隐藏中设置数据源,则需要使用刷新的数据进行设置,然后调用 DataBind 方法。
回答by yffu
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.
DataTable 中没有 Refresh 方法,但是在初始页面加载时调用 DataTable.EnableDynamicData(typeof(DataTable)) 解决了这个问题。
回答by jpsimard-nyx
Forcing Databind is normally done where the automatic DataBind is done in the PreRenderevent.
强制数据绑定通常在PreRender事件中完成自动数据绑定的地方完成。
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 :
通常,如果您在单击事件中执行了删除操作,则中继器应自行刷新,因为在预渲染中,页面上的控件是 DataBind()。这是微软的文档所说的:
PreRender : Before this event occurs each data bound control whose DataSourceID property is set calls its DataBind method.
PreRender :在此事件发生之前,设置了 DataSourceID 属性的每个数据绑定控件都会调用其 DataBind 方法。
So probably you affected Youritem.DataSource = List, but MS suggest doing YourItem.DataSourceID = List.ID, or something like that.
所以可能你影响了 Youritem.DataSource = List,但 MS 建议做 YourItem.DataSourceID = List.ID,或类似的东西。
Hope it helps
希望能帮助到你
回答by jpsimard-nyx
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.
我有一个类似的情况......一个转发器绑定到一个 xmlDataSource,都在一个 UpdatePanel 内。我想让用户一次输入一个名称,然后单击“添加”按钮以更新中继器中的列表。
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.
我将中继器和 xmlDataSource 上的“EnableViewState”设置为 False,并将 xmlDataSource 上的“EnableCaching”设置为 False。我设置了xmlDataSource 的Data 属性,为xmlDataSource 调用DataBind,设置repeater 的DataSourceID 属性,然后为repeater 调用DataBind。也许这太过分了……但它奏效了。也许这会有所帮助。
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.
更新:我发现通过在转发器控件上将 EnableViewState 设置为 False,我的 ItemCommand 事件不会触发。但我认为您只需要将数据源的 EnableViewState/EnableCaching 设置为 False...我已将转发器的 EnableViewState 设置返回为 True,现在一切看起来都很好。

