Skip to content
March 12, 2011 / pauldundon

DataControlField does not load ControlStyle from Viewstate

The Problem: You have a GridView and are manipulating the ControlStyle of one or more columns dynamically. Your changes are effective in the first render, but are lost on postback.

Possible Cause: It is a feature of / bug in the .NET framework that the ControlStyle is saved in view state but not reloaded.

Possible Solution: Override SaveViewState and LoadViewState in the closest container for the GridView (if you are using it on a page then override them in the page, if you are using it as part of a control then override them in the control). Assuming column is your troublesome column, use implementations like the following:

Protected Overrides Function SaveViewState() As Object
    Dim p As New Pair
    p.First = MyBase.SaveViewState()
    p.Second = CType(column.ControlStyle, IStateManager).SaveViewState()
    Return p
End Function

Protected Overrides Sub LoadViewState(ByVal savedState As Object)
    Dim p As Pair = CType(savedState, Pair)
    CType(column.ControlStyle, IStateManager).LoadViewState(p.Second)
End Sub

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: