Databound repeater nested in GridView will not update
-
16-02-2021 - |
Вопрос
I have a Repeater
nested inside of a GridView
. On the RowDataBound
event for the GridView
, I set the DataSource
(based on one of the row's columns) and then bind the Repeater
. This works fine for the initial load, however I need to be able to add new items to the Repeater
dynamically.
I append an item to the DataSource
, save it to the ViewState
, and where I would normally bind using a method call, I bind to the object saved to the ViewState
instead. The DataSouce
reflects the change, however the page does not.
What am I missing? I have the exact same setup on another page without the nesting and it works perfectly.
if (ViewState["RepeaterObj"]!=null)
{
rpt.DataSource=(IList<DataTransferObject>)ViewState["RepeaterObj"];
}
else
{
rpt.DataSource = controller.GetObj(param);
rpt.DataBind();
}
Решение
I ended up resolving the question by cutting out use of the ViewState
entirely, though I thought my temporary DataSource
would be lost across the postback
it wasn't. I ended up going with a class-level variable
which works perfectly. It seems I didn't properly understand what happens during a postback
.
Другие советы
First of all, you shouldn't be storing a datasource in ViewState. That's pretty much the worst place you could put it.
As for your problem, I would suggest either rebinding the GridView when new items are added to the repeater, or find the repeater in the event that saves the new record and rebind it there.
I think the problem is you are not rebinding the the Repeater
. You say you change how you bind to look at the ViewState
object but are you actually triggering the Bind
to occur? It sounds like you are not and the page is just reloading with the current data stored with the control's ViewState
.
Make sure you are calling your Repeaters
bind event explicitly to sure it is getting rebound.
EDIT: I suspect it might have something to do where you might need to rebind your GridView
and not just the Repeater
.