Scenario:
- Custom collection of business objects with public properties
- Bound directly to an asp.net 2.0 GridView
- Columns declared as a mix of BoundFields and TemplateFields
- A TemplateField with an asp.net control in the ItemTemplate is included
- Form postback button after the gridview posts it back to itself
The user can then perform actions on the control in the ItemTemplate (check a checkbox, change a drop down (the type of control makes no difference)) before hitting the save button and thus causing a postback.
At the point of postback the gridview rows are looped through (before a rebind takes place so relying on viewstate/postback information) when this is done and a FindControl is done on the row to get the control, it appears that the wrong control is found (one row ahead of where it should be, so the loop identifying Row 2 appears to bring back Row 3s control and values).
However this problem only occurs on the first postback, in subsequent postbacks the values appear to tally with the correct rows.
On closer inspection the client source is showing the same issue in this way (for the same control, in the first row after header row, ID value):
Its like the Cell ids are shifting to the correct numbering (3 to 2) after a single postback.
I can only assume this is in some way due to the Viewstate, Postback and Bind.
If anyone has any insights into what is causing this and how to fix it, it would be greatly appreciated as this is a serious sticking point on the project I'm working on!!
N.B. Viewstate, Paging and Sorting are enabled on the gridview (however the issue arises on the first page without sorting)
A header row is shown by setting HeaderText="blah..." on the TemplateField declarative tags (the issue arises with showHeader=False too)
The looping through gridviewrows before doing a rebind hopes to access any changes made by the user to the tabular incarnation of the custom business object collection so that they can be validated before being saved back to the custom collection and then rebound. Rebinding before going through the rows would lose that ability