2

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();
}
emd
  • 1,173
  • 9
  • 21
  • If I understand your edit correctly, and you have an answer for your question, please post it as an answer and accept it. – jwheron Oct 28 '11 at 20:51

3 Answers3

1

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.

emd
  • 1,173
  • 9
  • 21
0

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.

Kelsey
  • 47,246
  • 16
  • 124
  • 162
  • I am calling the repeater's bind event explicitly. As I mentioned it is almost the same code as the initial page load, just with a different datasource. The bind occurs either way and works normally when using a method to return the datasource rather than pulling it from the viewstate. – emd Oct 28 '11 at 17:57
  • @9ball are you sure your old method of binding isn't getting executed AFTER you are explicitly binding? Put a break point on the code and see if the old binding is happening afterwards. – Kelsey Oct 28 '11 at 18:20
  • The two bindings are on an if statement, only one executes. – emd Oct 28 '11 at 18:36
  • I think I do need to rebind the `GridView`, but I'm not sure how to do it without an infinite loop happening – emd Oct 28 '11 at 20:18
  • @9ball something is tiggering the need to reload this `Repeater` so bind the whole grid there and then in your `Repeater` `OnDataBinding` do your binding and look for the `ViewState` data for that specific row. I prefer to not use the `RowDataBound` event and implement databinding at the control level using `OnDataBinding` for the control. – Kelsey Oct 28 '11 at 20:21
0

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.

James Johnson
  • 45,496
  • 8
  • 73
  • 110
  • It isn't actually a substantial `DataSource`, just a list of tags with unique identifiers. I am using the `ViewState` temporarily until the user actually saves their edits. In order to add a new blank item, I have to rebind the repeater to a temporary list. Unless you have a better idea? – emd Oct 28 '11 at 18:31