Ecco l'errore:

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.
Ecco il codice:

codice:
    Private Sub Dtg_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Dtg.PreRender
        Dim DtgItem As DataGridItem
        For Each DtgItem In Me.Dtg.Items
            If BlnIsRicerca Then
                Dim TabPerc As New DataTable
                TabPerc = DirectCast(DirectCast(sender, DataGrid).DataSource, DataView).Table
                If DtgItem.ItemType = ListItemType.AlternatingItem Or DtgItem.ItemType = ListItemType.SelectedItem Or DtgItem.ItemType = ListItemType.Item Or DtgItem.ItemType = ListItemType.EditItem Then
                    BlnPrintPercorso = False
                    StrPercorso = DirectCast(TabPerc.Rows(IncItem)("gruppo"), String).ToUpper & " > " & DirectCast(TabPerc.Rows(IncItem)("ramo"), String).ToUpper & " > " & DirectCast(TabPerc.Rows(IncItem)("categoria"), String).ToUpper
                    If IncItem = 0 Then
                        Session("CurrPerc") = StrPercorso
                        BlnPrintPercorso = True
                    Else
                        If StrPercorso <> Session("CurrPerc") Then
                            Session("CurrPerc") = StrPercorso
                            BlnPrintPercorso = True
                        Else
                            BlnPrintPercorso = False
                        End If
                    End If
                    If BlnPrintPercorso Then
                        Response.Write(IncItem & ") " & Session("CurrPerc") & "
")
                        Dim NewItem As New DataGridItem(IncItem, IncItem, ListItemType.Item)
                        NewItem.ID = "NewRow_" & Me.Dtg.CurrentPageIndex & "_" & Me.IncItem
                        Dim NewTd As New TableCell
                        NewTd.ColumnSpan = 4
                        NewTd.Text = Session("CurrPerc")
                        NewTd.Width = New Unit("100%")
                        With NewTd
                            .BackColor = Color.FromName("#FFFFFF")
                            .Height = New Unit(30)
                            .HorizontalAlign = HorizontalAlign.Left
                        End With
                        NewItem.Cells.Add(NewTd)
                        Dim Tab As New Table
                        Tab = CType(Me.Dtg.Controls(0), Table)
                        Tab.Rows.AddAt(IncItem + IncItemBis + 2, NewItem) 
                        IncItemBis = IncItemBis + 1
                    End If
                    IncItem = IncItem + 1
                End If
            End If
        Next
    End Sub
è l'evento prerender di un datagrid. Ho provato a gestire il tutto negli eventi itemcreated e itemdatabound me il risultato è lo stesso.

Se commento la riga in rosso la pagina viene caricata correttamente, ma ovvimanete non mi aggiunge le righe al datagrid.

Da notare che l'errore avviene quando eseguo un postback della pagina, al primo caricamento infatti tutto funiona egregiamente.

sto impazzendo...

grazie

ciao