PRB: Error message: System.InvalidCastException: Specified Cast is not valid


Symptoms


When you view your ASP.NET page in the browser, you may receive the following error message:
Specified cast is not valid.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Specified cast is not valid.

Cause


This problem might occur on postback because the control tree hierarchy is re-ordered. On postback, the view state for controls is loaded based on the positions of the controls (indexes) in the control tree. The hierarchy that is created on postback must match the hierarchy that is used initially when the view state is saved.

Resolution


Make sure that the control tree is re-created on postback in the same order that it was saved at the end of the previous request.

Status


This behavior is by design.