Hi,
Don't know if anyone can help:
We have a web control that shows a collection as a series of
checkboxes. Preselected items are passed in and the user is free to
change the checked items. This is then read out of a property and
saved in the database.
However there is a problem persisting the state of the control between
postbacks. The user-selected items are stored in a class that
derives from arraylist, called UpdateAwareArra yList, which is just
aware of any changes made to it.
I'm storing this class in viewstate with the following code (in
VB.net)
Protected Overrides Function SaveViewState() As Object
Dim baseState As Object = MyBase.SaveView State()
Dim CombinedStates( 1) As Object
CombinedStates( 0) = baseState
CombinedStates( 1) = CType(arSelecte dItems, UpdateAwareArra yList)
Return CombinedStates
End Function
The class is declared as (in C#):
[Serializable()]
public class UpdateAwareArra yList : ArrayList, ISerializable
That seems to work, however when I try and get the class out of
viewstate, whats returned is a class of type ArrayList, not
UpdateAwareArra yList. I don't know why. As an attempt to work around
this I created a temporary arraylist to take the viewstate read, and
added its members to a variable of the right class:
Protected Overrides Sub LoadViewState(B yVal savedState As Object)
If Not (savedState Is Nothing) Then
Dim myState As Object() = CType(savedStat e, Object())
If Not (myState(0) Is Nothing) Then
MyBase.LoadView State(myState(0 ))
End If
Dim arTemp As ArrayList
If Not (myState(1) Is Nothing) Then arTemp = myState(1)
If arTemp.Count > 0 Then
arSelectedItems = New UpdateAwareArra yList()
Dim intTempCounter As Int32
For intTempCounter = 0 To arTemp.Count - 1
arSelectedItems .Add(arTemp(int TempCounter))
Next
End If
End If
End Sub
This works for the first postback, however on the second I get a
corrupt viewstate error on the page.
Don't know if anyone can help:
We have a web control that shows a collection as a series of
checkboxes. Preselected items are passed in and the user is free to
change the checked items. This is then read out of a property and
saved in the database.
However there is a problem persisting the state of the control between
postbacks. The user-selected items are stored in a class that
derives from arraylist, called UpdateAwareArra yList, which is just
aware of any changes made to it.
I'm storing this class in viewstate with the following code (in
VB.net)
Protected Overrides Function SaveViewState() As Object
Dim baseState As Object = MyBase.SaveView State()
Dim CombinedStates( 1) As Object
CombinedStates( 0) = baseState
CombinedStates( 1) = CType(arSelecte dItems, UpdateAwareArra yList)
Return CombinedStates
End Function
The class is declared as (in C#):
[Serializable()]
public class UpdateAwareArra yList : ArrayList, ISerializable
That seems to work, however when I try and get the class out of
viewstate, whats returned is a class of type ArrayList, not
UpdateAwareArra yList. I don't know why. As an attempt to work around
this I created a temporary arraylist to take the viewstate read, and
added its members to a variable of the right class:
Protected Overrides Sub LoadViewState(B yVal savedState As Object)
If Not (savedState Is Nothing) Then
Dim myState As Object() = CType(savedStat e, Object())
If Not (myState(0) Is Nothing) Then
MyBase.LoadView State(myState(0 ))
End If
Dim arTemp As ArrayList
If Not (myState(1) Is Nothing) Then arTemp = myState(1)
If arTemp.Count > 0 Then
arSelectedItems = New UpdateAwareArra yList()
Dim intTempCounter As Int32
For intTempCounter = 0 To arTemp.Count - 1
arSelectedItems .Add(arTemp(int TempCounter))
Next
End If
End If
End Sub
This works for the first postback, however on the second I get a
corrupt viewstate error on the page.
Comment