How to dynamically refresh a .NET databound repeater control
Asked Answered
F

5

8

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.

Frau answered 20/1, 2009 at 22:0 Comment(0)
S
3

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

Satinwood answered 20/1, 2009 at 22:20 Comment(1)
How can you call 'DataBind' from the source if, like in this case, the source is a 'List' ?Intone
P
1

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.

Pissarro answered 20/1, 2009 at 22:7 Comment(1)
Yes. I am refreshing the datasource and immediately calling the DataBind method.Frau
L
1

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.

Lonnielonny answered 26/12, 2014 at 19:13 Comment(0)
A
0

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

Afire answered 20/1, 2009 at 22:16 Comment(0)
L
0

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.

Lordling answered 29/1, 2009 at 16:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.