[Mono-bugs] [Bug 557223] System.InvalidCastException after second postback on a customize control.

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Mon Nov 30 07:56:16 EST 2009



Marek Habersack <mhabersack at novell.com> changed:

           What    |Removed                     |Added
             Status|NEW                         |CLOSED
         Resolution|                            |INVALID

--- Comment #1 from Marek Habersack <mhabersack at novell.com> 2009-11-30 13:56:14 CET ---
   This is result of a bug in Microsoft .NET which is not reproduced in Mono
and which happens to make such (bad) code work on .NET. The issue here is the
way you create and bind the DataBind control. What happens is that when you
bind the data in the click handler, the subsequent SaveViewState recursively
saves state of all the child controls of the grid (correct) but when a postback
comes back those controls do not yet exist - they will be created by datagrid
when it gets its chance to recreate the children and restore their state. But,
the state for the label to which you add the attribute is found in the
viewstate stream _before_ the grid fully restores state. So, the Label
control's serialized state is passed to another control (TableCell in your
case) and we get the exception in Mono. Why does it work in .NET, you ask? It's
because they use the same type (a Pair) to store state of all the controls and
so the state is silently accepted by the invalid control. As you can see, this
is a pretty serious design flaw and even a possible security threat on .NET's
part - one control should never, ever, get state of another control as
unpredictable things can happen. Microsoft apparently noted the problem as well
because in 2.0 they introduced the LoadViewStateByID property in Control
accompanied by the ViewStateModeById custom attribute that is supposed to
alleviate the problem, but it cannot solve it in your case since the control
which would have to be decorated with the attribute is TableCell. 
   So, to summarize - this is not a bug in Mono but an incompatibility in
internal implementation of a certain aspect of the framework. Mono
implementation is better as it exposes potentially serious bugs (like in your
test case) to the developer while .NET doesn't let you know in any way that
something bad is happening behind the scenes. I've seen this situation in other
software before and I decided not to "fix" this in Mono for the resons stated
above. _Always_ remember that viewstate must be symmetrical - that is the same
same number of controls and in the same sequence must exist when both saving
and restoring the state.
   The fix for you is to bind the datagrid very early in the process when
handling a postback.

hope that helps,

Configure bugmail: http://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.

More information about the mono-bugs mailing list