Thursday, July 24, 2008

ASP.NET - Render Hidden Fields at the Top of a Form with .NET 3.5 SP1

I just came across a nice feature in ASP.NET 3.5 SP1
that I didn’t realize was included. It’s always nice to discover
hidden gems from time to time. With ASP.NET 1.0, 1.1, 2.0 and 3.5
applications if a user or JavaScript function tried to do a postback
before all of the ViewState and EventValidation data was loaded in the
browser, a ViewState MAC validation error could be raised by the server.

In
ASP.NET 3.5 SP1 Microsoft provides a new attribute on the <pages>
element in web.config that now allows all hidden fields to be rendered
at the top of a form rather than at the bottom which can help to avoid
the ViewState MAC validation error when people perform a postback
operation before the page has finished loading. The attribute is named
renderAllHiddenFieldsAtTopOfForm (which does what it
says) and defaults to a value of true.