GridView DataBind Postback and Viewstate - Accessing rows after postback and before bind

Last post 06-06-2007 2:19 AM by Allen Chen – MSFT. 2 replies.

Sort Posts:

  • GridView DataBind Postback and Viewstate - Accessing rows after postback and before bind

    06-04-2007, 6:20 AM
    • Loading...
    • craigrowe
    • Joined on 06-04-2007, 9:41 AM
    • Posts 2

    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):

    • Pre First Postback: ctl00$Content$myGridView$ctl03
    • After First (and all other) Postbacks:  ctl00$Content$myGridView$ctl02


     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

     

    That's not a novel - that's the scribblings of a retard
  • Re: GridView DataBind Postback and Viewstate - Accessing rows after postback and before bind

    06-04-2007, 12:25 PM
    • Loading...
    • craigrowe
    • Joined on 06-04-2007, 9:41 AM
    • Posts 2

     If anyone even has any insights into the Viewstates of GridViews and how they effect the IDs of controls within the GridView that would be helpful... Would control IDs be changed by the viewstate after postback???

    That's not a novel - that's the scribblings of a retard
  • Re: GridView DataBind Postback and Viewstate - Accessing rows after postback and before bind

    06-06-2007, 2:19 AM

    Hi:

      Could you show us a simple demo that can reproduce your problem?

    Thanks

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Page 1 of 1 (3 items)
Microsoft Communities
Page view counter