locked
Failed to load viewstate. RRS feed

  • Question

  • User-751080518 posted

    Hi.

    I had problems with my custom field generator. If I put this line of code

    GridView1.ColumnsGenerator = new FieldGenerator(table, true);

    in page_init event, I get no errors, but event GenerateFields never fires.

    Then I found answer on this forum, that I have to move this line of code to Page_Load event, but now I ger this error, when I am selecting rows in gridview.

    Failed to load viewstate.  The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request.  For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request. 

     

    Friday, May 6, 2011 1:58 AM

Answers

All replies

  • User-121157572 posted

    Hi,

    Have a look at below article for the reason and resolution with detailed explanation,

    http://blog.typps.com/2008/01/failed-to-load-viewstate-typical.html

    http://forums.asp.net/t/1295517.aspx/1?Failed+to+load+viewstate+The+control+tree+into+which+viewstate+is+being+loaded+must+match+the+control+tree+that+was+used+to+save+viewstate+during+the+previous+request+For+example+when+adding+controls+dynamically+the+controls+added+during+a+post+back+

    Friday, May 6, 2011 4:36 AM
  • User-751080518 posted

    I don't think this is the answer I was looking for.

    In my case, if I comment line, or move it to Page_Init event

    GridView1.ColumnsGenerator = new FieldGenerator(table, true);

     everything works ok.

    Friday, May 6, 2011 5:39 AM
  • User-330204900 posted

    Hi borutsla, are you using VS2010 and .Net 4? if so have a look at my article here A New Way To Do Column Generation in Dynamic Data 4 this gets around having to add a fiedl generator to each page but doing it inside a cusom MetaModel

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 6, 2011 6:10 AM
  • User-526354650 posted

    This might be a result of having the GridView in a user control. Depending on where in the page lifecycle the user control is initialized, Page.Init event may have already fired and the Page_Init handler of the user control will not be called. I would stay away from the Page event handlers and try using Control events instead. For example, assigning the columns generator in the GridView.Init event handler is guaranteed to fire because Control.Init method is invoked as part of the "catch-up" process even if the control is added to the page late in the life-cycle. GridView.Init event is also a better place to do this because Page.Init event fires after Control.Init events and the view state tracking has already been turned on for the GridView, which will lead to errors during post-back.

    HTH

    Friday, May 6, 2011 9:06 AM
  • User-330204900 posted

    Hi Oleg, see this thread where David Ebbo has the answer http://forums.asp.net/t/1353129.aspx/1?Failed+to+load+viewstate

    Saturday, May 7, 2011 4:48 AM