I have an asp:FormView control bound to a datasource. Everything is working fine.
If I put the following code in the Init event:
Private Sub frmEdit_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles frmEdit.Init
Debug.WriteLine(frmEdit.Controls.Count)
End Sub
…..the update no longer works (no exceptions, it just reloads itself with the old data.)
However, if I change it to this:
Private Sub frmEdit_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles frmEdit.Init
Debug.WriteLine("hello world")
End Sub
….it works again. So, the difference is that I am accessing the frmEdit.Controls in the first example. My question is, why does this break it?
When you access a Control collection, you are triggering code ensuring that the child controls are initialized as well. For a FormView, Init is far too early in the lifecycle for this. You’re initializing controls before the ViewState is ready, so the controls aren’t going to be re-populated correctly.
You can access the controls later in the lifecycle and I think
frmEdit.Row.Controlsis what you’re after.